Multithreaded cURL wrapper The following is a class for curl wrapper.

This class will be called like:

     class curlWrapper {

private:
    CURLcode curlCode;
    CURL* curlHandle;
    struct curl_slist *header = NULL;

    MemoryStructA response;

    static std::atomic<int> numberOfCurlProcess;
    static std::atomic<bool> isGlobalInitCalled;
    static std::mutex curlGlobalkey;

public:
    curlWrapper(){

        curlGlobalkey.lock();

        if(!isGlobalInitCalled)
        {
            curl_global_init(CURL_GLOBAL_ALL); // this should be called only once in the entire program when a first object using this class.
            numberOfCurlProcess++;
        }
        else
        {
            numberOfCurlProcess++;

        }
        curlGlobalkey.unlock();

        //globalCurlInit();

        initializeMemoryStruct();

    }

    ~curlWrapper(){

        curlGlobalkey.lock();

        if(numberOfCurlProcess > 0)
        {

            numberOfCurlProcess--;
        }
        else if(numberOfCurlProcess == 0)
        {
 // this should be called only once in the entire program when a last object using this class.

            curl_global_cleanup();

        }
        curlGlobalkey.unlock();

    }

    int trace(CURL *handle, curl_infotype type,  char *data, size_t size, void *classPointer);
    int traceImpl(CURL *handle, curl_infotype type,  char *data, size_t size);

    CURL* curlInit();

    void curlClose();

    bool initializeConnection();

    bool sendRequest(const char* request);

    char* getResponse();

    void setMemorytoWrite();

    bool listen();

    void closeConnection();

    bool setURL(const char* url);

    bool setHttpHeader(const char * contentType, const char * charSet);

    void setTrace(bool trace);// If true trace will be displayd

    bool curlExec();

    void initializeMemoryStruct();

    void globalCurlInit();

    void globalCulrClean();

};

This class will be used as:

   class Accessor{
   public:
          curlWrapper *curl = new curlWrapper()

    };

main(){

   //somewhere in the main
      Accessor aa = new Accessor();
      Accessor bb = new Accessor();

     thread(aa).doProcess();
     thread(bb).dpProcess()

}

Did I call curl_global_init(CURL_GLOBAL_ALL) at first thread starts and only once? Similarly, did I call curl_global_cleanup() while the last thread ends?

Based on the code you have listed, the answer to both of your questions is: no.

curl_global_init(CURL_GLOBAL_ALL) won't be called only once as you're checking isGlobalInitCalled but not setting it to indicate the initialization shouldn't be done again. curl_global_cleanup() won't be called because you have no matching delete-expression for the destructors to get called (see the memory leaks portion of new expression).

Regarding how to make this work...

For the one-time-initialization, you could eliminate the use of the isGlobalInitCalled variable and just use numberOfCurlProcess instead (in the constructor) like this:

curlWrapper() {
    curlGlobalkey.lock();

    if(numberOfCurlProcess == 0)
    {
        curl_global_init(CURL_GLOBAL_ALL); // this should be called only once in the entire program when a first object using this class.
    }
    numberOfCurlProcess++;

    curlGlobalkey.unlock();

    //globalCurlInit();

    initializeMemoryStruct();
}

For the one-time-cleanup, I'd suggest using smart pointers which "enable automatic, exception-safe, object lifetime management". As to how code using smart pointers could look, that can depend on what the newest version of the C++ standard is that you're able to use. With C++14, I'd use code that instead looked more like:

class Accessor {
public:
    std::unique_ptr<curlWrapper> curl = std::make_unique<curlWrapper>();
};

main() {
    //somewhere in the main
    std::unique_ptr<Accessor> aa = std::make_unique<Accessor>();
    std::unique_ptr<Accessor> bb = std::make_unique<Accessor>();

    thread(aa).doProcess();
    thread(bb).doProcess();
}

There are other ways to solve the second problem too but if you're going to dynamically allocate instances, smart pointers is the way I'd suggest managing memory as they'll take care of calling the instances' destructors for you.


Generate secure URL safe token using Microsoft.AspNetCore.WebUtilities;
using System.Security.Cryptography;

namespace UserManager.Cryptography
{
    public class UrlToken
    {
        private const int BYTE_LENGTH = 32;

        /// <summary>
        /// Generate a fixed length token that can be used in url without endcoding it
        /// </summary>
        /// <returns></returns>
        public static string GenerateToken()
        {
            // get secure array bytes
            byte[] secureArray = GenerateRandomBytes();

            // convert in an url safe string
            string urlToken = WebEncoders.Base64UrlEncode(secureArray);

            return urlToken;
        }

        /// <summary>
        /// Generate a cryptographically secure array of bytes with a fixed length
        /// </summary>
        /// <returns></returns>
        private static byte[] GenerateRandomBytes()
        {
            using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider()) {
                byte[] byteArray = new byte[BYTE_LENGTH];
                provider.GetBytes(byteArray);

                return byteArray;
            }
        }
    }
}

I've created the above class (C#, .Net Core 2.0) to generate a cryptographically secure string token that is URL safe, so it can be used in an URL without the necessity to be encoded.

I will use that token as a GET parameter (e.g. www.site.com/verify/?token=v3XYPmQ3wD_RtOjH1lMekXloBGcWqlLfomgzIS1mCGA) in a user manager application where I use the token to verify the user email or to recover a user password.

The above link will be sent as email to the user that has requested the service.

I store the token into a DB table with an associated expiration datetime.

I've seen other implementations on this and other sites but all seem to be unnecessarily complicated.  Am I missing something?

Minor suggestions:

public class UrlToken

The class has no instance data, so it could be made static:

public static class UrlToken

Microsoft's Naming Guidelines and their Framework Design Guidelines suggest not using underscores and also using PascalCasing for constants, so

    private const int BYTE_LENGTH = 32;

could be:

    private const int ByteLength = 32;

However, even that name doesn't tell us much of what it is for. Let's try again:

    private const int NumberOfRandomBytes = 32;

Typo/misspelling in the XML doc comment: "encoding" is written as "endcoding".

There is mixed curly brace formatting. Microsoft guidelines (see links above) suggest the opening and closing curly braces should be on their own line.

        using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider()) {

to:

        using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
        {

By the way, kudos to you on your proper use of the using construct! Looks fantastic!


Object Oriented Modular Closure The following is some code I just wrote.  I keep wondering as always what I can do to make this more efficient. Please help me make this code object oriented and if anyone could help me write automated or even non automated tests for my widget...

Also is this considered a closure? I don't think it is but I would love to know how to produce better quality/reusable code.  Oh- bonus points for any corrections to my techniques used as well.  I'm really trying to step my JS game up and I know it needs work...

var ExplodedContentWidget = $('[data-widget~="exploded-content"]');
// search for existance of the exploded widget in DOM
if (ExplodedContentWidget[0]){
    var ExplodedDefaultEl = $('[data-function~="default-state"]');
    var ExplodedEl = $('[data-function~="exploded-state-1"]');
    var ExplodedEl2 = $('[data-function~="exploded-state-2"]');
    var CloseButton = $('.button-close');
    ExplodedEl.hide(); // default exploded content is hidden
    ExplodedEl2.hide();
    CloseButton.hide();
    ExplodedContentWidget.on('click',function(){
            ExplodedEl.slideToggle('slow');
    });
    var FullScreenViewButton = $('button');
        FullScreenViewButton.on('click',function(){
            ExplodedDefaultEl.hide();
            ExplodedContentWidget.addClass('exploded-fullscreen-state');
            ExplodedEl2.slideToggle('slow');
            CloseButton.fadeIn();
                ExplodedEl2.on('click',function(){
                    ExplodedContentWidget.removeClass('exploded-fullscreen-state');
                    ExplodedDefaultEl.show();
                    ExplodedEl.toggle();
                    ExplodedEl2.toggle();
                    CloseButton.hide();
        });
     });
    }

PS: Here is the fiddle

From a once over:

Do not use data-function~="exploded-state-1", instead assign id's and search by id, this is the common ( and faster, better ) approach
One single var statement with comma separated variables is considered better
I would go for if (ExplodedContentWidget.length){ instead of if (ExplodedContentWidget[0]){
Variable names should follow lowerCamelCase so ExplodedEl -> explodedEl, the exception to that is for jQuery results so you could also use $exploded1
This :

ExplodedEl.hide(); // default exploded content is hidden
ExplodedEl2.hide();
CloseButton.hide();

I would have addressed with a css class that hides these elements
You indent the code after var FullScreenViewButton = $('button');, dont do that. Consider using the jsbeautifier website
$('button'); <- This is code waiting to go wrong, what if you have more than 1 button. Give the button an id, and select on that id
Your code passes JsHint.com nicely, well done
Also is this considered a closure? Your inline functions for .on('click' are closures.


Line and column tracker for text files A class that keeps track of lines and columns in a text file. I use it in a parser so that when an error occurs, I can print out the line and column at which it occured.

Here's line col.hpp

#ifndef line_col_hpp
#define line_col_hpp

#include <cctype>
#include <cstdio>
#include <cassert>

///Keeps track of lines and columns in text.
///Great for writing error messages in parsers
class LineCol {
public:
  using LineType = unsigned int;
  using ColType = unsigned int;
  static const ColType SIZE_OF_TAB = 8;
  static const LineType FIRST_LINE = 1;
  static const ColType FIRST_COL = 1;

  explicit LineCol(LineType line = FIRST_LINE,
                   ColType  col  = FIRST_COL);

  ///Move line and col according to the char.
  ///Call this at the end of the loop with the char you just processed
  void update(char);
  ///This calls update(char) for each char in the null terminated string
  void update(const char *);
  ///This calls update(char) for the first n chars in the string
  void update(const char *, size_t);
  ///Sets line to FIRST_LINE and col to FIRST_COL
  void reset();

  LineType getLine() const;
  ColType getCol() const;
  const char *getStr() const;
private:
  LineType line;
  ColType col;
};

#endif

Here's line col.cpp

#include "line col.hpp"

LineCol::LineCol(LineType line, ColType col)
  : line(line), col(col) {
  assert(line >= FIRST_LINE);
  assert(col >= FIRST_COL);
}

void LineCol::update(char c) {
  switch (c) {
    case '\t':
      col += SIZE_OF_TAB;
      break;
    case '\n':
      line++;
      col = FIRST_COL;
      break;
    //vertical tab and form feed do the same thing on my system
    case '\v':
    case '\f':
      line++;
      break;
    case '\r':
      col = FIRST_COL;
      break;
    case '\b':
      //backspace doesn't move up lines
      //when backspacing tabs it treats them as spaces so it only
      //moves back one char
      if (col != FIRST_COL) {
        col--;
      }
      //i don't have to put a break here but should I even though we'll
      //fallthrough to another break?
    case '\0':
    case EOF:
      break;

    default:
      if (std::isprint(c)) {
        col++;
      }
  }
}

void LineCol::update(const char *str) {
  assert(str);
  while (*str) {
    update(*str);
    str++;
  }
}

void LineCol::update(const char *str, size_t size) {
  assert(str);
  while (size) {
    update(*str);
    str++;
    size--;
  }
}

void LineCol::reset() {
  line = FIRST_LINE;
  col = FIRST_COL;
}

LineCol::LineType LineCol::getLine() const {
  return line;
}

LineCol::ColType LineCol::getCol() const {
  return col;
}

const char *LineCol::getStr() const {
  //The largest 64 bit integer value is 18446744073709551615 which is
  //20 characters. 20 + ':' + 20 + '\0' = 42
  static char str[42];
  std::snprintf(str, 42, "%u:%u", line, col);
  return str;
}

Heres an example of how the class is used.

const char *buf;
size_t size;
LineCol lc;
for (const char *c = buf; c < buf + size; c++) {
  //do stuff with the character
  //maybe print lc.getStr()
  lc.update(*c);
}

How could this be improved? Are there any special characters that I've missed?

Overall I'd say this code is very easy to read. I can see quickly how everything works. Here are a few suggestions:
Only Make Public What's Necessary
There are 3 update() functions. Looking at them, it's not clear what the purpose of them is. The first one doesn't look like it should be public at all. It changes the internal state of the object, but doesn't update the string it comes from, which is confusing. It means that a caller could repeatedly call it with the same character from a string and it would count incorrectly. I'd make that version private.
I'd also make the argument to void update(char) be const since it's not modified.
Naming
Name Your Method Arguments!
What do the method arguments represent? Presumably for the 2 update() methods that take a pointer to a character, they represent a string to be parsed. They should have a name like parseString or something similar.
Improve Your Method Names
The method name update() doesn't tell me anything about what the function does. Any method that modifies the state of an object updates it in some way. A better name might be parse() or advanceLineAndColumn() or something that says what it actually does.
The getStr() method is very deceptive because it looks like it gets a pointer to the string this object is parsing. But it doesn't! Since this is C++, you'd be better served by making an operator<<() method or free function to print out your object.
Even reset() could be a little more descriptive. Perhaps something like resetParsing() or resetLineAndColumn(). Consequently, why would I want to use reset() instead of just creating a new instance? They seem fairly lightweight, so what's the advantage? If there is one, maybe that would be helpful in renaming the method.
Fix This Bug
In updated(str, size); you have a potential memory stomper. If size is greater than the actual length of str, you'll call update(str); and str might point to memory you don't have permission to read, causing a crash. Alternatively it might point to other data in your app and cause other problems such as incorrect line and column values.
All of this could be avoided if you...
Use std::string
Since you're using C++, you should be using C++'s string class to handle strings. It eliminates the possibility of running off the end of the string because you can access it using the at() method, which will throw an exception if you try to access a character outside the length of the string.


Javascript: Google Analytics Track Outbound Page Views I've researched this morning how to use Google Analytics to track outbound page views. However, I think the implementation might be a bit difficult on our website (as we have hundreds of outbound links)

Here's what google recommends.

/**
* Function that tracks a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
Check out example.com

As you can see, attaching an onClick attribute to every single anchor tag you want to track is fine, IF YOU ARE JUST NOW DEVELOPING A WEBSITE!

To remedy this issue, I have written some code to loop through every anchor tag on the page, and determine if the anchor is external or not. In this script, *.pdf need to be included. Here my custom script:

var anchors = document.getElementsByTagName("a");

  for (i = (anchors.length - 1); i > -1; i--) {
   var anchorHREF = anchors[i].getAttribute("href");
   if (anchorHREF != "" && anchorHREF != null) {
    var doSetOnClick = false;

    if (anchors[i].hostname != document.location.hostname) {
     doSetOnClick = true;
    }

    if (anchorHREF.indexOf(".pdf") > -1) {
     doSetOnClick = true;
    }

    if (doSetOnClick) {
     anchors[i].setAttribute("onClick", "javascript: trackOutboundLink('" + anchorHREF + "');")
    }
   }
  }

Note: This code fires on DOM Ready.

I am not asking for code review on the google part, just my custom loop. Do you feel as though my loop is going to have good browser support? >IE8? How could I Improve this code?

I would write this code like:

Array.prototype
  .slice
  .call(document.getElementsByTagName("a")) // Get all links from the page
  .forEach(function (link) { // Loop through them
    const isExternal = link.hostname != document.location.hostname;  // Check if is external
    const isPDF = link.href.indexOf(".pdf") > -1; // Check is is PDF
    if(isExternal || isPDF){
      link.setAttribute("onClick", "javascript: trackOutboundLink('" + link.href + "');"); // set onClick attribute
    }
  })

Update #1

Using ES6, you can do something like this:

// Get all the links from the page
const links = [...document.getElementsByTagName('a')];

// "Explain what a pdf link is"
const isPDF = link => link.href.endsWith('.pdf');

// "Explain what an external is"
const isExternal = link => link.hostname != document.location.hostname;

// "Explain what links you want to target"
const target = link => isPDF(link) || isExternal(link);

// "Explain what should be done to those links"
const onClick = link => link.setAttribute('onClick', `javascript: trackOutboundLink('${link.href}');`);

// Filter and do the work
links.filter(target).map(onClick);

I would suggest the second method, because it's much more clear, pure and very easy to test.


Vue + Vuex + Firebase serverless realtime chat I am "discovering" Firebase and I'm trying create simple chat with Vue, Vuex and Firebase. But because I'm just starting out with Vue, I need code review or advices from more experienced programmers. I need to know whether this code is properly structured - if it is well written in "Vue way" - and how to make my components better - more universal / better separated from rest of the code.

jsFiddle

var uid = null

var firebaseConfig = {
  apiKey: "AIzaSyDGYiab8xoIXIgbFSmLSwXubNL2T3Rr6AQ",
  authDomain: "first-steps-6abb2.firebaseapp.com",
  databaseURL: "https://first-steps-6abb2.firebaseio.com"
}

var store = new Vuex.Store({
  state: {
    messages: []
  },
  mutations: {
    addMessage (state, message) {
      state.messages.push(message)
      if (state.messages.length > 10) {
        state.messages.shift()
      }
    }
  }
})

var listMessages = {
  template: `

        {{message.time}} {{ message.text }}

  `,
  data () {
    return {
      messages: this.$store.state.messages
    }
  }
}

var addMessage = {
  template: `

      Add new:
      <input id="message" @keyup.enter v-model="message">
      <button @click="sendMsg">Send</button>

  `,
  data () {
    return {
      message: ''
    }
  },
  methods: {
    sendMsg () {
      messages.push().set({
        uid: uid,
        content: this.message,
        timestamp: firebase.database.ServerValue.TIMESTAMP
      })
      this.message = ''
    }
  }
}

firebase.initializeApp(firebaseConfig)
firebase.auth().signInAnonymously()
firebase.auth().onAuthStateChanged(function (user) {
  if (user) { uid = user.uid }
})

new Vue({
  el: '#app',
  store,
  components: {
    listMessages,
    addMessage
  }
})

var db = firebase.database()
var messages = db.ref('messages')
var query = messages.orderByChild('timestamp').limitToLast(10)

query.on('child_added', function (data) {
  store.commit('addMessage', {
    uid: data.val().uid,
    text: data.val().content,
    time: moment(data.val().timestamp).format('hh:mm:ss')
  })
})
html, body {
  margin: 0;
  height: 100%;
}
.container {
  min-height: 100%;
  position: relative;
}
.list-messages {
  position: absolute;
  box-sizing: border-box;
  width: 100%;
  top: 10px;
  margin: 0;
  padding: 10px;
  list-style: none;
}
.list-messages span {
  color: lightblue;
}
.add-message {
  text-align: center;
  position: absolute;
  width: 100%;
  bottom: 10px;
}

  <list-messages></list-messages>
  <add-message></add-message>

The code looks pretty straight-forward and I don't spot any obvious simplifications. I would suggest that you add error handling - especially for the case when uid is not null and remains that way. Perhaps an error message should be added if the request for the appropriate value fails.

Since ecmascript-6 is used (e.g. with the template literals) const could be used instead of var for any variable that doesn't get re-assigned (e.g. the components, db, etc.) and let for other variables (e.g. uid).

You could also consider providing a key attribute on the v-for where the messages are displayed, but the markup is quite simple for those so it may not add any benefit.

  It is recommended to provide a key with v-for whenever possible, unless the iterated DOM content is simple, or you are intentionally relying on the default behavior for performance gains.1

1https://vuejs.org/v2/guide/list.html#key


Normalize a GSM short message text I am working to improve this script, which should prepare a text to be sent as a GSM short message using only the GSM 7-bit character coding with Basic Character Set Extension.

import logging
import traceback
import unicodedata
logger = logging.getLogger(__name__)

class IsGSM:

    def __init__(self):
        self.gsm = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?" \
      "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§¿abcdefghijklmnopqrstuvwxyzäöñüà"
        self.ext = "^{}\\[~]|€"
        self.toBeNormalized = '\xa0'+'\xc2'
        self.ascii_gsm = {}
        self.ascii_map()
        self.normalize_dict = {}
        self.normalize_map()

    def ascii_map(self):
        for c in self.gsm:
            #if ord(c) < 256:
            self.ascii_gsm[ord(c)] = True
        for c in self.ext:
            #if ord(c) < 256:
            self.ascii_gsm[ord(c)] = True

    def normalize_map(self):
        for c in self.toBeNormalized:
            self.normalize_dict[c] = True

    def _is_gsm(self, content):

        template = content

        try:
            template.encode('windows-1252')
        except UnicodeEncodeError:
            return False

        for c in content:
            if ord(c) not in self.ascii_gsm:
                return False
        return True

    @classmethod
    def is_gsm(cls, content):
        instance = IsGSM.instance()
        return instance._is_gsm(content)

    @classmethod
    def normalizeBodyContent(cls, content):
        instance = IsGSM.instance()
        return instance._normalizeBodyContent(content)

    @classmethod
    def instance(cls):
        """Singleton like accessor to instantiate backend object"""
        if not hasattr(cls, "_instance"):
            cls._instance = cls()
        return cls._instance

    def _normalizeBodyContent(self, content):
        try:
            target_content = [c for c in content]
            content_length = len(target_content)
            for ith in range(content_length):
                if target_content[ith] in self.normalize_dict:
                    target_content[ith] = unicodedata.normalize("NFKD",target_content[ith])

            content = "".join(target_content)
        except Exception as e:
               logger.error('Exception occurred normalizing content {0}'.format(content))
               traceback.print_exc()
               raise e
        return content

if __name__ == "__main__":
    print(IsGSM.is_gsm("Est a punto de restablecer la contrasea de su cartera de Kutxabank. Por favor, introduzca la sigui..."))
    print(IsGSM.is_gsm("This should fix the problem. Your content xxxxx from ÄA K COMPANY LTD will be delivered by xxxx today.Track your content at  some_url@&!"))

I can refactor the _normalizeBodyContent method like this:

def _normalizeBodyContent(self, content):
    try:
        target_content = [c for c in content]
        content_length = len(target_content)
        for ith in self.normalize_dict:
            target_content = target_content.replace(ith,
                                         unicodedata.normalize("NFKD", ith))
        content = "".join(target_content)
    except Exception as e:
        logger.error('Exception occurred normalizing content {0}'.format(content))
        traceback.print_exc()
        raise e
    return content

Is this an improvement on the original, in-terms of processing time and readability?

I'm particularly interested in comparing the lookup via in and overwrite against using the replace() library function.  Review on any portion of the whole script is also appreciated.

1. Review

Normally, a class is used to implement a collection of objects with similar behaviour, and an object is used to implement some kind of persistent thing or data structure. But what kind of thing is an instance of the IsGsm class? It doesn't look as though it corresponds to any kind of thing.

What you really have here is a function (IsGsm.is_gsm) that determines if text can be encoded in GSM 03.38. It's clear that this is a function rather than a method, because it does not use its cls argument. So it would be clearer and simpler if this were a function. In Python, not everything needs to be part of a class!

See Jack Diederich's talk "Stop Writing Classes" for more about this problem.
A new ascii_gsm data structure gets built each time you call IsGsm.is_gsm, but each time the data structure is the same. It would be simpler if this were a global variable. Then it would only have to be constructed once.
The ascii_gsm data structure is poorly named: it is a mapping whose keys are Unicode code points for characters that can be encoded.
The values of the ascii_gsm data structure are always True. This is a waste of space: if you just need the keys, then Python has a set datatype.
The code in the post thinks that FORM FEED cannot be encoded:

>>> IsGSM.is_gsm('\N{FORM FEED}')
False

but according to the "Basic Character Set Extension" table in Wikipedia, FORM FEED can be encoded as the two bytes 1B 0A.
I think it would be clearer and less risky to use Python's Unicode named escapes, and write \N{ESCAPE} instead of \x1b. The former is easier to read, easier to check, and Python will raise an error if you make a typo:

>>> '\N{ESACPE}'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-9: unknown Unicode character name

With hexadecimal escapes, Python won't help you if you write \x1c by mistake for \x1b.
But can ESCAPE really be encoded as 7-bit GSM? It looks to me as though it cannot — Wikipedia says:

  Note that the second part of the table is only accessible if the GSM device supports the 7-bit extension mechanism, using the ESC character prefix. Otherwise, the ESC code itself is interpreted as a space, and the following character will be treated as if there was no leading ESC code.

So whether the device supports the extension mechanism or not, ESCAPE cannot be encoded.
It's not clear why you want to normalize \xc2. This is LATIN CAPITAL LETTER A WITH CIRCUMFLEX, and when normalized as NFKD this becomes LATIN CAPITAL LETTER A followed by COMBINING CIRCUMFLEX ACCENT. But COMBINING CIRCUMFLEX ACCENT can't be encoded as 7-bit GSM, so the normalization doesn't help. I think this needs explanation, or perhaps just removing.

2. Revised code

I think that it would make more sense here to build a codec. The idea is to write an encode function that encodes Unicode text as 7-bit GSM, using the same interface as Python's own codecs.encode. Then callers will be able to write code like this to detect text that can't be encoded, using the "strict" error handler:

try:
    gsm_encoded_bytes = encode(text, 'strict')
except UnicodeEncodeError:
    # Can't encode text as GSM

This interface also supports more graceful handling of errors, for example if the "replace" error handler is used, then the output will have 3F when a character can't be encoded:

>>> encode('abcω', 'replace')
b'abc?'

Here's a start on an implementation:

import codecs

# Name of the codec.
_NAME = 'gsm03.38'

# GSM 03.38 Basic Character Set
_BASIC_CHARACTERS = (
    "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\N{ESCAPE}ÆæßÉ"
    " !\"#¤%&'()*+,-./0123456789:;<=>?"
    "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§"
    "¿abcdefghijklmnopqrstuvwxyzäöñüà")

# GSM 03.38 Basic Character Set Extension. Note that CR2 and SS2 are
# not Unicode characters, so don't appear in this table.
_EXTENSION_CHARACTERS = {
    '\N{FORM FEED}': 0x0A,
    '^': 0x14,
    '{': 0x28,
    '}': 0x29,
    '\\': 0x2F,
    '[': 0x3C,
    '~': 0x3D,
    ']': 0x3E,
    '|': 0x40,
    '€': 0x65,
}

# These are not in the GSM 03.38 specification but we provide them for
# convenience.
_ADDITIONAL_CHARACTERS = {
    '\N{NO-BREAK SPACE}': 0x20,
}

# Build mapping from Unicode character to GSM 03.38 encoding
_CHARMAP = {c: bytes([i]) for i, c in enumerate(_BASIC_CHARACTERS)}
del _CHARMAP['\N{ESCAPE}']
for char, encoding in _EXTENSION_CHARACTERS.items():
    _CHARMAP[char] = bytes([0x1B, encoding])
for char, encoding in _ADDITIONAL_CHARACTERS.items():
    _CHARMAP[char] = bytes([encoding])

def encode(input, errors='strict'):
    error_handler = codecs.lookup_error(errors)
    result = bytearray()
    i = 0
    while i < len(input):
        c = input[i]
        if c in _CHARMAP:
            result.extend(_CHARMAP[c])
            i += 1
        else:
            exc = UnicodeEncodeError(_NAME, input, i, i + 1, "no encoding")
            replace, i = error_handler(exc)
            for c in replace:
                result.extend(_CHARMAP[c])
    return bytes(result)

With some more work we could add a decode function that decodes 7-bit GSM as Unicode, using the same interface as Python's own codecs.decode function. Then we will be able to register our functions with the codec registry, allowing users to write code like:

try:
    gsm_encoded_bytes = text.encode('gsm03.38', 'strict')
except UnicodeEncodeError:
    # Can't encode text as GSM

But in fact there's already a GSM 03.38 codec on the Python Package Index, and it probably makes sense to use that instead of writing your own.


Fetching data via jquery and adding items to a list Super-duper javascript noob here. I'm trying to fetch public repos from GitHub, and add them to a dl. I know this won't work on every website due to cross-site scripting, but GitHub offers a JSON-P API. I'm fairly certain that I'm doing this mostly the right way (it works), but the append function looks ugly, and I'm not sure if the way I'm triggering the script is considered best practice.

gh-project.js:

function get_github_public_repos(username) {
    var the_url = "https://api.github.com/users/" + username + "/repos"

    $.ajax({
      url: the_url,
      dataType: 'jsonp',
      type: 'get',
      success: function(data) {
        for(i = 0; i < data.data.length; i++){
          var repo = data.data[i];
          if (!repo.fork) {
            $('#gh-projects').append("
" + repo.name + "" + "
" +
            "
" + repo.description + "
" );
          }
        }
      }
    });
}

projects.html:

Thanks for the pointers.

Your code looks good, and I'm not going to suggest comestic changes. However:

the_url is not informative enough, try to find a variable that conveys that this url will let you fetch the repositories of an user.
Did you consider that repo.name could contain a ' which would cause you to output wrong html? Fortunately, GitHub prevents such things to happen.
Declare var i; at the top of your success function. Here's why.
The way you're building your html string is dangerous.  It's also hard to read and easy to get wrong.

As Philip suggested, using some sort of template would be a good idea, but there's no way to do this in standard jQuery (jQuery templates have been deprecated, and the way forward seems to be JsRender which is not yet Beta). Fortunately, your code is not big enough for you to need that. Instead, use attr(), text() and addClass() wisely:

var dt = $('<dt>').attr('href', repo.url).attr('title', repo.name).text(repo.name);
var dd = $('<dd>').addClass('project-description').text(repo.description);
$('#gh-projects').append(dt).append(dd);


Comparing two bash caches I was working on making a cache in bash and I found this link - But this guy is attempting to generate new bash functions by introspecting their source and generating a new one as a cache. This seems to me, way overkill. Here is my implementation and it works just fine:

cache() {
  local file
  file="/tmp/$(printf "%s" ${@})"
  if [[ -f "${file}" && $(($(date +%s) - $(date -r "$file" +%s))) -le 1800 ]]; then
    cat "${file}"
  else
    ${1} ${@:2} | tee "${file}"
  fi
}

mkurls() {
  local url="${1:?You must supply a URL}"
  local batchSize=${2:-100}
  local repoCount
  repoCount=$(curl -fsnL "$url?pagelen=0&page=1" | jq -re .size)
  local pages=$(((repoCount / batchSize) + 1))

  printf "$url?pagelen=${batchSize}&page=%s\n" $(seq 1 "$pages")
}

bbusers() {
  _users() {
    mkurls https://api.bitbucket.org/2.0/teams/twengg/members/ |
      xargs -n 30 -P 20 curl -fsnL | jq -ser '.[].values[] | "\(.display_name), \(.links.self.href)" ' | sort
  }
  cache _users $@
}

and here are the results:

$ time bbusers &> /dev/null
#output

real    0m5.670s
user    0m0.184s
sys     0m0.099s

$ time bbusers &> /dev/null # seconds later

real    0m0.053s
user    0m0.010s
sys     0m0.017s

As you can see, it's literally 100x faster. If I delete the file behind the cache:

$ rm -fR /tmp/_users

$ time bbusers &> /dev/null

real    0m4.924s
user    0m0.170s
sys     0m0.082s

It goes right back to normal. So, how can this be improved and what am I missing that warrants such a wildly complicated approach as the other guy has?

As the "guy" in question, I'm happy to discuss my approach :) feel free also to file issues on GitHub if you have feedback.

You are certainly correct that decorating functions as bash-cache does isn't necessary, but the intent is to simplify the development experience - to memoize a function all you have to do is make a call to bc::cache, and bash-cache (generally) handles the rest. No need to keep separate functions in sync or wrestle with naming schemes. Whether that's a feature or overkill is in the eye of the beholder, but I find it very flexible and expressive :)

As you've shown it's not too complicated to implement a reasonable caching mechanism in Bash, but the devil is really in the details. With bash-cache I've addressed a number of very subtle issues that most caching implementations fail to handle, including:

stdout and stderr are both cached and preserved separately (your approach only caches stdout, other approaches merge the two streams with >&)
the return code of the function is also preserved, few other implementations I've seen do this (correctly)
avoids a common gotcha with command substitutions ($(...)), which are actually lossy
stale cache data is regularly cleaned up in the background
caches are separated by user and chmod-ed to only be readable by that user; a global cache risks leaking sensitive data

Since this is CodeReview.SE, some tips on your approach:

Be sure to consistently quote all variables, including arrays like "$@"; without quotes Bash will expand variables and arrays with word-splitting, which is rarely desirable and often leads to bugs. In your code it's likely that whitespace arguments will break things. It's a great idea to run any shell code through ShellCheck and address the issues it flags.
file="/tmp/$(printf "%s" ${@})" is unsafe, as it munges arguments together without a delimiter, e.g. it will treat  foo 12 3 and foo 1 23 as the same set of arguments. It also will fail if any arguments contain invalid filename characters (notably /), which is (part of) why bash-cache hashes the arguments.
It's probably a good idea to asynchronously delete the cache file when it's too old, instead of making multiple date calls in subshells to determine whether the file is still valid. I'd have to benchmark to be sure but I suspect it will be faster.
${1} ${@:2} isn't necessary, just say "$@" (note the quotes)
Bash doesn't support nested/anonymous functions, so what you're doing in bbusers is actually just re-defining _users in the top-level namespace on every invocation, which isn't necessary or helpful. Just define _users as a normal function and then: bbusers() { cache _users "$@"; }.


Finding cointegrated pair with minimal t-score The function findMinimal() receives a tuple from a list which contains 171 tuples, each of them go into the function one by one and, for each tuple, it searches the best pair via minimalTscore out of those that match by industry (approx. 20 names, for each one). How can I make the function to run faster? At the moment, it's quite slow.

def findMinimal(tup): #Finds cointegrated pair with minimal t-score
    stockPair = []
    stock = tup[0]
    industry = tup[1]
    minimalTscore = 0
    stockListLocal = [i for i in stockList if i[0] != stock]
    clf = linear_model.LinearRegression()
    for i in stockListLocal:
        if i[1] == industry and stock != i[0]:
            Y = data[stock][datesIterable[startTrain:endTrain]]
            X = data[i[0]][datesIterable[startTrain:endTrain]]
            clf.fit(np.array(X).reshape(len(X),1), np.array(Y).reshape(len(Y),1))
            beta_hr = clf.coef_[0]
            alpha = clf.intercept_[0]
            residuals = Y - beta_hr*X - alpha
            cadf = ts.adfuller(residuals)

            if cadf[4]['1%'] > cadf[0]:
                if hurst(residuals) < 0.4:
                    if minimalTscore == 0:
                        minimalTscore = cadf[0]
                        stockPair = i[0]
                    else:
                        if cadf[0] < minimalTscore:
                            minimalTscore = cadf[0]
                            stockPair = i[0]
    if (stockPair,stock) not in pairsOnlyStocks and len(stockPair) > 0:
        pairs.append((stock, stockPair, minimalTscore)) #Y, X, tscore
        pairsOnlyStocks.append((stock, stockPair)) #this is only for filtering

1. Review

As Mathias Ettinger points out in comments, it is hard to review this code because we have no idea what the global variables stockList, linear_model, data, datesIterable, startTrain, endTrain, ts, hurst, pairsOnlyStocks and pairs mean.
There's no docstring. What does this function do? What parameters does it take? What does it return? What side effects does it cause?
Code that depends on global variables is hard to understand, hard to test, and hard to parallelize. It would be better to turns some of these variables into function parameters, or into attributes on objects belonging to a class for which findMinimal is a method.
The code filters stockList on i[0] != stock to get stockListLocal. But then it iterates over stockListLocal and filters again on the same condition. This is wasted work.
Each time findMinimal is called, it looks at all the stocks and filters on i[1] == industry. If you maintained a list of stocks by industry then you would could save a lot of work.
pairsOnlyStocks seems to be a list (it has an append method). Lists do not have an efficient membership test: Python has to compare the item against each list member in turn. So (stockPair,stock) not in pairsOnlyStocks will be slow. It would be better to use a set.
minimalTscore starts at zero, which requires the code to have a special case if minimalTscore == 0:. Better to use Python's built-in min function and avoid the special case.
Instead of indexing:

stock = tup[0]
industry = tup[1]

use tuple assignment:

stock, industry, *_ = tup

But note that this only works in Python 3. In Python 2 you can write:

stock, industry = tup[:2]

and I think this is still clearer than indexing.
Similarly, instead of:

cadf = ts.adfuller(residuals)

use tuple assignment with wildcards:

score, _, _, _, critical_values, *_ = ts.adfuller(residuals)

There's duplicated code of the form:

data[stock][datesIterable[startTrain:endTrain]]

Putting this in a function would reduce duplication and make the code easier to follow, and it would be another place to add a docstring.
Instead of:

.reshape(len(X),1)

write:

.reshape(-1, 1)

The computation of residuals:

Y - beta_hr*X - alpha

suggests that X and Y are already NumPy arrays and so you don't need to call np.array on them.
The array Y gets computed each again for each stock in the list. It could be computed just once by moving it out of the loop.
The arbitrary values '1%' and 0.4 should be given names and documented, for example by turning then into optional function parameters.

2. Revised code

This code implements some of the suggestions above:

def stock_data(stock):
    """Return stock data for [explanation here]."""
    return data[stock][datesIterable[startTrain:endTrain]]

def add_best_pair(stock_tuple, significance='1%', max_hurst_exponent=0.4):
    """Given a tuple (stock0, industry, ...), find the tuple (stock1,
    industry, ...) in the global stocks such that the pair
    (stock0, stock1) has the smallest Augmented Dickey-Fuller score
    among all such pairs, and add it to the global pairs if not
    already present in either order.

    Optional parameter significance gives the required significance
    level for the Augmented Dickey-Fuller test, and max_hurst_exponent
    gives the maximum Hurst exponent for the autocorrelations of the
    residuals.

    """
    stock0, industry0, *_ = stock_tuple
    Y = stock_data(stock0)
    clf = linear_model.LinearRegression()
    def scores():
        for stock1, industry1, *_ in stocks:
            if industry0 == industry1 and stock0 != stock1:
                X = stock_data(stock1)
                clf.fit(X.reshape(-1, 1), Y.reshape(-1, 1))
                beta_hr = clf.coef_[0]
                alpha = clf.intercept_[0]
                residuals = Y - beta_hr*X - alpha
                score, _, _, _, critical_values, *_ = ts.adfuller(residuals)
                if (score <= critical_values[significance]
                    and hurst(residuals) <= max_hurst_exponent):
                    yield score, stock1
    try:
        _, stock1 = min(scores())
    except ValueError:
        # None of the candidate pairs passed the statistical tests.
        return
    if stock1, stock0 not in pairs_set:
        stock_pair = stock0, stock1
        pairs.append(stock_pair)
        pairs_set.add(stock_pair)

3. Performance

It's hard to see how to implement this without carrying out the linear regression and Augmented Dickey-Fuller test on all pairs of stocks in each industry. But here are some ideas on speeding it up:

Are the statistical tests symmetric? That is, do we expect to get the same score when comparing stock A with stock B as we do when we compare stock B with stock A? If so, it would be possible to cache the score computed for (A, B) and look it up when computing (B, A). That would save half the work.
It might be possible to use a rough test to quickly reject pairs of stocks that won't pass the statistical tests. For example, you could start by testing daily/weekly/monthly averages, and only if those test pass do you go on to test the high-resolution data.
It might be straightforward to parallelize the computation of statistical tests, using concurrent.futures.ThreadPoolExecutor in Python 3, or the concurrent.futures backport in Python 2.


Illumination as a way of instantiating new objects Basically, what I mean by illumination is having a method create an object where the caller supplies all the dependencies.  This allows us to hide our new keyword behind a dependency on an Illuminator, allowing us to much more easily test our code.

Yes, a little bit of reflection is involved, but not too much (IMO).

Please tell me what you think of this type (is the intent behind it clear? et alia):

namespace xofz.Illumination
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    public class Illuminator
    {
        public virtual T Illumine<T>(params object[] dependencies) where T : class
        {
            var constructors = new LinkedList<ConstructorInfo>(
                typeof(T).GetConstructors().OrderByDescending(ci => ci.GetParameters().Length));
            var list = new List<object>(dependencies);
            foreach (var ctor in constructors)
            {
                var parameters = ctor.GetParameters();
                var valuesNeeded = new List<object>(list.Count);
                foreach (var parameter in parameters)
                {
                    for (var i = 0; i < list.Count; ++i)
                    {
                        if (!parameter.ParameterType.IsInstanceOfType(list[i]))
                        {
                            continue;
                        }

                        valuesNeeded.Add(list[i]);
                        list.Remove(list[i]);
                        break;
                    }
                }

                if (valuesNeeded.Count == parameters.Length)
                {
                    list = valuesNeeded;
                    break;
                }

                // if the objects don't line up with this constructor, start over with the next one
                list = new List<object>(dependencies);
                valuesNeeded.Clear();
            }

            return (T)Activator.CreateInstance(typeof(T), list.ToArray());
        }
    }
}

Edit 2: added some ordering to GetConstructors().  So far, it only orders by the number of parameters.

Edit: here is an example of what you can do with an Illuminator.  Say you have a class that has a lot of dependencies.  We can new up all the dependencies of the dependencies, and then actually assign the fields of the class through illuminator.Illumine() calls:

public class MegaBigClass
{
    public MegaBigClass(Illuminator illuminator)
    {
        var a = new A(); // new in a constructor?! Blasphemy!
        var b = new B();
        var c = new C();
        var x = new X();
        var y = new Y();
        var z = new Z();

        // then actually set the fields with the illuminator
        this.abc = illuminator.Illumine<AbcComposite>(a, b, c);
        this.xyz = illuminator.Illumine<XyzComposite>(x, y, z);
    }

    private readonly AbcComposite abc;
    private readonly XyzComposite xyz;
}

Contemplate how testable this code is.  Because Illumine is virtual, it is easy to create a fake or alternate Illuminator for test code to enable us to exactly specify what gets put into the fields.  Look at how we can use new all we want in our constructors, but we need to make sure to assign fields only through illuminator.Illumine() calls.

Could I wrap the entire implementation into a single Activator.CreateInstance() call?  Perhaps, but I feel like doing it this way instead allows for imperfect calls to illuminator.Illumine<T> that still supplies all the necessary dependencies.

I suppose we are talking about DI.

Based on your example, I can not see the advantages of the Illuminator class. If MegaBigClass depends on AbcComposite and XyzComposite, the right way would be to pass that objects to the constructor. In your case, you have to pass the dependencies of AbcComposite and XyzComposite to MegaBigClass to be able to create the composite objects within that class. However, MegaBigClass should not be responsible for creating that objects.

If MegaBigClass must be able to create the composit objects during runtime, Illuminator serves as something like a "generic factory" (Btw: In that case, I would call it factory so that other developers know what it is). For that case, the Illuminator provides a way to create objects without using new where the concrete creation logic can be changed for testing.

The logic of the Illuminator is (correct me if I am wrong): Search the first constructor from an unordered list that's signature matches to any sub set of the passed arguments.

Well, lets assume we will use the Illuminator that way:

I am not able to define which constructor should be used. If the class to create has a parameterless constructor that is returned first in .GetConstructors(), the class will be created always by the parameterless constructor.

MSDN - GetConstructors()

  The M:System.Type.GetConstructors method does not return constructors in a particular order, such as declaration order. Your code must not depend on the order in which constructors are returned, because that order varies.

because that order varies.! Therfore, it is possible that running the code twice, the class creates objects on different ways.
Even if the order of the returned constructors will not change (or will be ordered first), what is if the class to be created becomes a new constructor somedays... That means that all the places where the Illuminator is used, objects may be created using another constructor! That may result in funny bugs ;)

Conclusion

I would really suggest NOT to use that class! There are excellent frameworks that adress the problems you are tring to solve (e.g. NInject factory extensions for creating factories, Moq for mocking objecs to enable testability ...).


Cookie authentication I have to write a little social network for a case study at university. I have done some simple webapps before but nothing that required authentication, login and so on. So I wrote this little application after reading several guides on how this is usually implemented.

What I came up with is a simple login.html file in my server's public directory that contains a form with username and password. After submitting, the server looks up the username in a HashMap and checks firstly if the user exists and secondly if the password is correct. If both criteria are met, I set a cookie via response that contains a random UUID that the server keeps track of in an ArrayList. Lastly, the before filter checks if any protected page is being accessed, and if so, checks the requests cookies and looks them up in the ArrayList. If the cookie is known to the server I grant access to whatever is under /protected/.

Apart from encryption, is this code doing what I want? For me it looks like it works: login sets the cookie, logout removes it, /protected/ path can't be accessed without a known cookie. But I don't exactly know if I am missing something critical that might allow people to access pages without my knowledge.

package co.selim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import spark.Spark;

public class Router {
    public static void main(String[] args) {
        Map<String, String> credentials = new HashMap<>();
        credentials.put("root", "passw0rd");

        List<String> tokens = new ArrayList<>();

        Spark.staticFileLocation("public");

        Spark.exception(Exception.class, (exception, request, response) -> {
            exception.printStackTrace();
        });

        Spark.before("/protected/*", (req, res) -> {
            if (!tokens.contains(req.cookie("token")))
                Spark.halt(401, "You can't access this page.");
        });

        Spark.post("/login", (req, res) -> {
            String username = req.queryParams("user");
            String password = req.queryParams("password");

            String storedPassword = credentials.get(username);
            if (storedPassword != null && password.equals(storedPassword)) {
                String token = UUID.randomUUID().toString();
                tokens.add(token);
                res.cookie("token", token, 3600);
            } else
                return "Username or password wrong.";
            return "";
        });

        Spark.get("/logout", (req, res) -> {
            String token = req.cookie("token");
            if (tokens.remove(token)) {
                res.removeCookie("token");
                return "Successfully logged out.";
            } else
                return "You were not logged in.";
        });

        Spark.get("/protected/controlPanel", (req, res) -> {
            return "<button>Launch nukes</button>";
        });
    }
}

I'm not familiar with Spark. It's obvious that this code isn't thread-safe (ArrayList isn't thread-safe, and tokens is accessed without any explicitly locking), but I don't know whether Spark provides the thread safety.

The cookie handling is missing two important parameters:

                res.cookie("token", token, 3600);

There are various overloads which take boolean secured, boolean httpOnly, and ideally you would set both of those to true. If you can't set secured because you're using a cheap web host which doesn't support HTTPS, at least set httpOnly.


Prime number generation - SPOJ This is a program to generate prime numbers for a given range. The sieve of Eratosthenes algorithm was implemented, but still the SPOJ says time limit exceeded. Where can some optimizations be made?

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

import java.util.*;
import java.lang.*;
class Main
{
    public static void prime(int m, int n)
    {
    boolean arr[] = new boolean[n+1];
    for(int i = 0; i < arr.length; i++)
        arr[i] = true;
    for(int p = 2; p*p <= n; p++)
    {
        if(arr[p] == true)
        {
            for(int j = p*2; j <= n; j += p)
                arr[j] = false;
        }
    }
    for(int i = m; i <= n; i++)
    {
        if(arr[i] == true)
            System.out.print(i + " ");
    }
}
public static void main (String[] args) throws java.lang.Exception
{
    Scanner in = new Scanner(System.in);
    int test = in.nextInt();
    int m = 0,n = 0,flag = 0;
    while(test > 0)
    {
        m = in.nextInt();
        n = in.nextInt();
        prime(m,n);
        test--;
    }
}
}

When generating prime numbers with the sieve of Eratosthenes, you can treat 2 as a special case.  All other prime numbers are odd, so you can increment by 2:

for(int p = 3; p*p <= n; p += 2)

When you find a prime number \$p\$, you have already eliminated all multiples of prime numbers below \$p\$.  This means starting at \$2p\$ and going up is eliminating already eliminated multiples.  You can start at \$p^2\$, and since you are only concerned with odd numbers, go up multiples of \$2p\$

        for(int j = p*p; j <= n; j += 2*p)

When looping over the array for printing, you could again skip all even numbers, by going up by 2.  But make sure you start on an odd number!

m |= 1;
for(int i = m; i <= n; i += 2)

If you reversed your sense of the flag array, so true means "not prime", you wouldn't have to loop through the array to set all elements to true at the start.  They would start off as false, which would mean "prime".

Marking all prime numbers on one pass, and then looping through your marked array printing out prime numbers is looping twice for possibly no reason.  You can simply print out the prime numbers as you find them, if it is inside the required range.  It would require some juggling, and optimization tricks to ensure you print out a number only when required, and only exactly once.

You generate the sieve 10 times, if you have 10 test cases.  Maybe you could examine all 10 test cases, find the largest end point, and generate the sieve once???


Redux: button click potentially fires 3 action for different reducers I’m using Redux and thunk, I have a list component

class List extends Component {
  render() {

    //code
  }
  return(
    <Aux>
      <Button
        type="button"
        click={() => this.props.addNew(data)}
        btnType="add" >
        Add
      </Button>
      <Button
        type="button"
        click={() => this.props.deleteB(id, type)}
        btnType="Delete" >
        Delete
      </Button>
      <Button
        type="button"
        click={() => this.props.editStart(id)}
        btnType="edit" >
        Edit
      </Button>
      <Button
        type="button"
        click={() => this.props.editSave(data, type)}
        btnType="save" >
        save
      </Button>
    </Aux>
  )
}

const mapStateToProps = state => {
    return {
        editing: state.list.editing
    };
};

const mapDispatchtoProps = dispatch => {
  return {
    deleteB: (id, type) => {dispatch(actions.deleteB(id, type))},
    editStart: (id) => {dispatch(actions.editStart(id))},
    editSave: (data, type) => {dispatch(actions.editSave(data, type))},
    addNew: (data) => dispatch(actions.addNew(data))
  };
};

export default connect(mapStateToProps, mapDispatchtoProps)(List);

A reducer

const addNew = ( state, action ) => {
  //immutable state
  //add new
  //return updated state
};

const deleteB = ( state, action ) => {
  //immutable state
  //delete
  //return updated state
};

const editStart = ( state, action ) => {
  //immutable state
  //update editing to true
  //return updated state
};

const editSave = ( state, action ) => {
  //immutable state
  if(action.type === state.editing.type) {
    //update object with user data
  } else {
    //delete old data same code of deleteB
    //add data user like addNew
    //update editing to false
  }
  //return updated state
};

const reducer = ( state = initialState, action ) => {
  //switch case
};

export default reducer;

When the user clicks on the “save” button if the type of the data changed the reducer uses the same functions that the buttons “add” and “delete” uses.
I’ve created an utility function for deleting and adding data, but It still looks ugly.

I was wondering if when the user clicks “Save” it is better to call a function in the List component that calls “addNew” and “deleteB” and  finally “editSave” to only update the state for the “editing” state property.

I think that a reducer needs to know only the state he needs to update, so editSave should only update the editing slice of state, and I need to reuse the others reducers.
But I don’t know if there is a better way, or if I wrote a bad pattern for the reducers.

I was wondering if when the user clicks “Save” it is better to call a function in the List component that calls “addNew” and “deleteB” and finally “editSave” to only update the state for the “editing” state property.

I would avoid this. Ideally, all your logic should live either the reducer or the thunk, NOT in components. This way, your logic is in one place, are easily and uniformly testable, and are not affected by things such as re-implementation of the UI (i.e. changing of components, replacing the UI library, etc).

Reducers are just pure functions - they take in old state and an action, and they return new state. As long as you follow that basic principle, you're good. When you call a reducer from another reducer, you're effectively just composing reducers - which isn't a strange concept in Redux. So it's not weird to have something like this:

export const add = (state, action) => { ... }

export const edit = (state, action) => { ... }

export const delete = (state, action) => { ... }

export const someWeirdCaseOfAddEditDelete = (state, action) => {
  if(action.type === state.editing.type) {
    return edit(state, action)
  } else {
    const intermediateState = add(delete(state, action), action)
    const finalState = doMoreStuffWith(intermediateState, action)
    return finalState
  }
}

As an added bonus, when the time comes when someWeirdCaseOfAddEditDelete starts to deviate from your regular add, edit and delete, you can simply replace its implementation and its tests without having to meddle with the other three reducers.

To address your concerns in the comments:

  I thought that was an anti-pattern because it's like dispatching an action inside a reducer that got executed from an action.

Calling dispatch in a reducer is the antipattern. But composing functions (i.e. breaking up your reducer into subfunctions that deal with specific parts of the state) is totally fine.

It may be easier to wrap your head around the idea by dropping the assumption that the common operation is a reducer. Think of it as just a common utility function:

const thatCommonFunction = (somePartOfTheState) => ({
  thatOnePropertyYouNeedToChange: { ... }
})

const reducer1 = (state, action) => ({
  ...state,
  ...thatCommonFunction(state.some.part)
})

const reducer2 = (state, action) => ({
  ...state,
  ...thatCommonFunction(state.some.part)
})

  Every time I call "deleteB" or "add" from "someWeirdCaseOfAddEditDelete" the state will update, before returning the intended "finalState".

If the state updates before all of your reducers return the new state to Redux, then there's something wrong with your code.

Redux receives the updated state only after it runs through all the reducers. The state should never update while execution is still in the reducer.

Under the hood, at the very basic, Redux does something like this:

const createStore = (rootReducer, initialState = {}) => {
  let currentState = initialState;
  const subscriptions = [];

  return {
    dispatch(action) {
      // This part is where control is handed over to your reducers.
      currentState = rootReducer(currentState, action);

      // Only after the line above should your state be updated.

      // After that one line, the UI is informed of the update.
      subscriptions.forEach(s => s(currentState))
    },
    subscribe(fn) {
      subscriptions.push(fn)
    }
  })
}


Implementation of database access class [PHP] I programmed a class for database access for my REST service. According to my understanding it should be implemented as singleton, because there can be a lot of simultaneous requests. Is there everything okay with this approach?

Is there a way to handle the getData... functions better? They basically do all the same thing, expect for the SQL query and the different parameters for the query.

Is it good to store the connection data (username, password..) in a JSON file?

Note that I've modified some lines from the actual code (SQL-queries and function names), because I think they are not relevant for my questions.

class DBManager {
protected static $con;
protected static $_instance = null;
protected function __construct() {
    $data = self::getConnectionData ();
    $host = $data ["host"];
    $name = $data ["name"];
    self::$con = new PDO ( "pgsql:host=$host;dbname=$name", $data ["user"], $data ["password"] );
}
protected function __clone() {}

public static function getInstance() {
    if (null === self::$_instance) {
        self::$_instance = new self ();
    }
    return self::$_instance;
}
protected function getConnectionData() {
    $raw = file_get_contents ( "conf/database.json" );
    $json = json_decode ( $raw, true );
    return $json;
}
public function getData1($year) {
    $query = "...";
    $stmt = self::$con->prepare ( $query );
    $stmt->bindParam ( 1, $year );
    $stmt->execute ();
    $result = $stmt->fetchAll ( PDO::FETCH_ASSOC );
    return $result;
}
public function getData2($year, $month) {
    $query = "...";
    $stmt = self::$con->prepare ( $query );
    $stmt->bindParam ( 1, $year );
    $stmt->bindParam ( 2, $month);
    $stmt->execute ();
    $result = $stmt->fetchAll ( PDO::FETCH_ASSOC );
    return $result;
}

// more getData functions...
}

The reason for singletons

First, you've got some misunderstandings about what singletons accomplish.  PHP automatically keeps all variables separate from request to request, so the fact that you have simultaneous requests makes no difference for the singleton pattern.  Rather, the singleton pattern makes sure that you only have one instance of a particular class inside a single request.  Your constructor is protected, and so the only way to get a DB connection object is through the static getInstance method, which makes sure there is only ever one instance.  The relevancy of this to a DB connection class is that if the system can create more than one instance of your DB connection class per request, and if your DB connection class opens a new connection to the database everytime it is created, then you can potentially get many open connections to MySQL from a single HTTP request.  That's a quick way to overload your database's open connection pool and end up with failed requests.  It doesn't have anything to do with having multiple requests at the same time.

The reason to not use singletons

That being said, while this is a good stepping stone to a better application, it shouldn't be your final stepping point.  Singletons are, in essence, a design pattern that should be considered deprecated.  The reason is because they break dependency injection.  If you're new to this concept (we all were at some point) then it is absolutely worth a lot of googling and reading.  Dependency injection, other than making your code more flexible and easier to maintain, also makes it possible for you to mock and test your code thoroughly (via unit and integration tests).  It is a completely different way of building an application, so I wouldn't expect you to just throw everything away and start over.  However, it is never to soon to start learning, so you can do yourself a favor and start reading about these things now.

Oftentimes with dependency injection you will have a dependency injection container that handles the details for you, and these containers usually have a way of flagging classes as "singleton" classes, which means that the dependency injection container will never initialize more than one.  At that point in time though, the classes don't implement the singleton pattern: they act like normal classes with a normal constructor, and rely on the DI container to make sure there is only one.

Passwords in config files

This is fairly common, and is normally fine.  Just make sure that the file with your DB credentials isn't committed to your code repository.

Public functions

The other problem you have is that you are trying to stick your data access method directly into your DB connection class.  You don't want to actually do that.  For an application of almost any size, you are going to quickly end up with far too many methods, and this class will become impossible to read/update/maintain.  It's all about the separation of concerns.  Ideally, your DB connection class would just be a simple wrapper around the PDO object, and your models classes (depending on how you organize your application), would then be responsible for using the DB connection class to execute their own queries, as is relevant to them.


ADT stack with a dynamic array (revision 1) Here's the second draft of my ADT stack code which I posted here before after carrying out most of the improvements suggested there.

I decided to expose a function called resize_stack to adjust the size of the stack as desired by the user instead of doing it automatically.

stacklib.h

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifndef STACKLIB_H
#define STACKLIB_H

/* generic type pushed item_to the stack */
/* in this case it's just an int */
typedef int item_t;

/* our stack data type */
typedef struct {
    size_t size;
    size_t capacity;
    item_t *elements;
} stack;

/* interface to our new stack data type */
stack* create_stack(size_t capacity);
int push_stack(stack *mystack, item_t data);
item_t* pop_stack(stack *mystack);
void delete_stack(stack *mystack);
stack* resize_stack(stack *mystack, size_t capacity);

#endif

stacklib.c

#include "stacklib.h"

/* a helper function to handle exceptions */
void handle_error(const char* message)
{
    if(errno) {
        perror(message);
    } else {
        printf("Error: %s\n", message);
    }

    exit(1);
}

stack* create_stack(size_t capacity)
{
    stack *stack_ptr = malloc(sizeof(stack));

    if(stack_ptr == NULL)
        handle_error("Error while allocating memory.");

    item_t *elements_ptr = malloc(capacity*sizeof(item_t));

    if(elements_ptr == NULL) {
        free(stack_ptr);
        handle_error("Error while allocating memory.");
    }

    stack_ptr->size = 0;
    stack_ptr->capacity = capacity;
    stack_ptr->elements = elements_ptr;

    return stack_ptr;
}

void delete_stack(stack *mystack)
{
    free(mystack->elements);
    free(mystack);
}

/* Returns 0 on success, 1 on failure */
int push_stack(stack *mystack, item_t data)
{
    /* stack is full! */
    if(mystack->size == mystack->capacity)
        return 1;

    mystack->elements[mystack->size++] = data;
    return 0;
}

/* Returns a pointer to the popped data or NULL if the stack is empty */
item_t* pop_stack(stack *mystack)
{
    if(mystack->size == 0) {
        return NULL;
    } else {
        return &mystack->elements[--mystack->size];
    }
}

/* Returns a pointer to the adjusted stack or NULL on failure */
stack* resize_stack(stack *mystack, size_t capacity)
{

    item_t *tmp = realloc(mystack->elements, sizeof(item_t) * capacity);
    if(tmp == NULL)
        return NULL;
    mystack->elements = tmp;
    mystack->capacity = capacity;

    if(mystack->size > capacity)
        mystack->size = capacity;

    return mystack;

}

main.c

#include "stacklib.h"

int main(void){
    stack *my_stack = create_stack(20);
    item_t i;
    for(i = 0; i < 20; i++) {
        push_stack(my_stack, i);
    }

    resize_stack(my_stack, 10);

    item_t res, *item ;
    while((item = (item_t*)pop_stack(my_stack)) != NULL) {
        res = *item;
        printf("data: %d\n", res);
    }
    delete_stack(my_stack);
    printf("last item popped was  %d\n", res);
    return 0;
}

It looks very nice, code is clean and readable, not much else to change, I think...

This is more of a personal preference, not an universal recommendation: One of the things I like about C is having fine control over memory allocations, thus making it a very good choice for systems with limited memory or very rudimentary memory allocators. Because of that, I like to avoid allocating memory whenever possible, so I would not allocate each stack instance on the heap with malloc. Rather, let the user decide where to place it, be on the program stack, as a global or heap-allocated. To do that change, you'd just have to remove that allocation from create_stack and one free from delete_stack, taking a pointer to an external object in create:

bool create_stack(stack* mystack, size_t capacity);

That might seem like a little thing, but forcing the user to dynamically allocate a stack object when it doesn't have to is suboptimal and can be a problem on systems that don't rely on virtual memory and have fragmentation issues.

I strongly recommend that you use booleans instead of error codes when a function has only two possible outcomes, success or failure. There's no universal agreement on the meaning of error codes, e.g. some return 0 on success, some return 1, some return -1 on error, and so on. This can be a major source of confusion. Use the bool type from <stdbool.h> whenever it makes sense.

Avoid repeating types in your malloc calls. E.g.:

item_t *elements_ptr = malloc(capacity*sizeof(item_t));

Can be refactored to:

item_t *elements_ptr = malloc(capacity * sizeof(*elements_ptr));

Notice that you can use the variable name in the sizeof expression, in this case dereferenced, since we want the size of the type itself, not the size of a pointer. Makes your life easier if you change the name of the type you are allocating; one less place to be updated.

Make sure to print errors to stderr. That's the traditional error output and allows users to filter errors from normal program output. Just replace the printf call in handle_error by fprintf(stderr).

Two last tips:

You might want to allow the users of your stack to provide their own error handler. You can achieve that with function pointers.

All functions receive pointers to stack, which could be passed in as null by error or due to some memory allocation failure. You can assert that they are not null to trap errors early and ease the debugging efforts.


Dynamically generated controls based on data from dictionary I have attempted the GUI controls creation in a prompt based on data from dictionary.  Is this the best way of implementing this?

import sys
from PyQt4 import QtCore, QtGui

class MessageBox(QtGui.QDialog):
    """docstring for MessageBox"""
    def __init__(self, data=None, parent=None):
        super(MessageBox, self).__init__(parent)
        self._data = data
        self.buildUi()

    def buildUi(self):
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setSpacing(10)
        for index, (key, values) in enumerate(self._data.iteritems()):
            getLbl = QtGui.QLabel("Get", self)
            label = QtGui.QLabel(key, self)
            chkBox = QtGui.QCheckBox(self._data[key][0], self)
            chkBox.setToolTip("Click here to get the book")
            version = QtGui.QSpinBox( self)
            version.setValue(self._data[key][-1])
            version.setRange(self._data[key][-1], 12)
            self.gridLayout.addWidget(getLbl, index, 0)
            self.gridLayout.addWidget(label, index, 1)
            self.gridLayout.addWidget(chkBox, index, 2)
            self.gridLayout.addWidget(version, index, 3)
        self.layout = QtGui.QVBoxLayout()
        self.okBtn = QtGui.QPushButton("OK")
        self.layout.addLayout(self.gridLayout)
        self.horLayout = QtGui.QHBoxLayout()
        self.horLayout.addStretch(1)
        self.horLayout.addWidget(self.okBtn)
        self.layout.addLayout(self.horLayout)
        self.setLayout(self.layout)

class MainWindow(QtGui.QMainWindow):
    """docstring for MainWindow"""
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # self.widget = FormWidget()
        self._data = {
                'Contact':['Carl Sagan', 2],
                'End of Faith':['Sam Harris', 7],
                'On Mars':['Patrick Moore', 1],

                }
        self.btn = QtGui.QPushButton("Hello", self)
        self.btn.clicked.connect(self._launchMessageBox)

        self.show()

    def _launchMessageBox(self):
        dlg = MessageBox(self._data)
        dlg.exec_()

def main():
    app = QtGui.QApplication(sys.argv)
    window =  MainWindow()
    window.show()
    # window.raise_()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

This will work, but it may be hard to scale.  At a minimum, I'd create a separate class for the work you're doing in the enumerate loop so that the dialog class is only concerned with populating a widget list and the details of what the widgets look like internally are handled in a class that just pops out book widgets - that will make for cleaner code and easier maintenance.

The more 'modern' way to do this is some variant on the MVC (Model-View-Controller) or MVVM (Model -view - viewmodel) patterns.  The basic idea is the same -  create a factory which produces display widgets for the items in a list of some kind. The main difference its that the MVVM pattern encourages you to split out the different aspects of the problem -- managing the contents of the list, displaying parts of the list, and interactng with the list items -- into distinct parts of the code (the 'model', the 'view' and the 'viewmodel' or 'controller') respectively.  (There's a whole lot of programmer-on-programmer controversy about MVC vs MVVM - background here.)

In this example, going with an MV* pattern offers  facilities like filtering a list ('show only books from 2013'), enabling/disabling list items ('gray out the books over $50') without lots of tedious looping codes.  Plus you could potentially do things like design your widgets in the QT designer for better layout, etc without touching the code directly.

All this lets you decouple the UI functionality (views, filters, appearance) from the data.  As your lists get bigger, and the need to manage their contents gets more complex, this is a big help in staying sane.

Yasin from TAO has nice, detailed Youtube tutorial on this approach.


Decided to learn Scala. Here's an attempt at a Cloth simulation I have never done anything functional before. These two functions are hideous to look at. II think the first step to do is extract the inner bits out and then map over my arrays. What else can I do to make this code a bit more functional?

https://github.com/dbousamra/scalacloth/blob/master/src/cloth/Cloth.scala

Specifically:

  def verletIntegration() = {

    for (row <- grid) {
      for (p <- row) {
        if (p.stuck) p.setCurrentPos(p.getPreviousPos)

        var multiplyByTime = Tuple2(p.getForces * timestep * timestep, gravity * timestep * timestep)
        var minusPrevPos = Tuple2(p.getCurrentPos.getX - p.getPreviousPos.getX, p.getCurrentPos.getY - p.getPreviousPos.getY)
        var together = Tuple2(multiplyByTime._1 + minusPrevPos._1 , multiplyByTime._2  + minusPrevPos._2)
        p.setPreviousPos(p.getCurrentPos)
        p.setCurrentPos(new Position(p.getCurrentPos.getX + together._1, p.getCurrentPos.getY + together._2))
      }
    }
  }

  def satisfyConstraints() = {
    for (row <- grid) {
      for (p <- row) {
        if (p.stuck) p.setCurrentPos(p.getPreviousPos)
        else {
          var neighbors = p.getNeighbors
          for (constraint <- neighbors) {
            val c2 = grid(constraint.getX)(constraint.getY).getCurrentPos
            val c1 = p.getCurrentPos
            val delta = Tuple2(c2.getX - c1.getX, c2.getY - c1.getY)
            val deltaLength = math.sqrt(math.pow((c2.getX - c1.getX), 2) + math.pow((c2.getY - c1.getY),2))
            val difference = (deltaLength - 1.0f) / deltaLength
            val dtemp = Tuple2(delta._1 * 0.5f * difference, delta._2 * 0.5f * difference)
            p.setCurrentPos(new Position(c1.getX + dtemp._1.floatValue, c1.getY + dtemp._2.floatValue))
            grid(constraint.getX)(constraint.getY).setCurrentPos(new Position(c2.getX - dtemp._1.floatValue, c2.getY - dtemp._2.floatValue))
//          }

          }
        }
      }
    }
  }

for (row <- grid) {
  for (p <- row) {

//should be written as:

for(row <- grid; p <- row) {

Tuple2("bla", 42)

//should be written as

("bla", 42)

I don't really understand why you use tuples when you have already a class for doing exactly such kind of math, namely Position.

The code could be more functional by using immutable case classes for Positions (but it depends on the problem if this is the right thing to do):

case class Position(x: Float, y: Float) {
    def -(that:Position) = Position(this.x - that.x, this.y - that.y)
    def +(that:Position) = Position(this.x + that.x, this.y + that.y)
}

Then you can write things like pos1 + pos2. Note that you don't need the getter methods, as you can write pos1.x, and no setter methods, as you can write and pos1.copy(x=12). The new isn't required any longer: val p = Position(1, 3).

How to proceed with the refactoring would depend on your decision if you keep Position as it is, or if you want to go with immutable Positions.

[Edit]

Here is how I would refactor it:

Run.scala:

package cloth

object Run {

  def main(args: Array[String]): Unit = {

    import javax.swing.JFrame

    val test = new Screen
    val frame = new JFrame("scalacloth")
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setPreferredSize(new java.awt.Dimension(640, 720))
    frame.getContentPane().add(test)
    test.init
    frame.pack
    frame.setVisible(true)
  }

}

import processing.core._

class Screen extends PApplet {

  val cloth = new Cloth(
    rows = 20,
    columns = 20,
    gravity = 0.001f,
    timestep = 0.8f,
    fixedParticles = List(Coordinate(19,0))
  )

  override def setup() {
    cloth.createGrid()
    size(640, 720)
    background(255)
    smooth()
    noStroke()
    fill(0, 102)
  }

  override def draw() {
    background(255)
    fill(255)
    stroke(0)

    def drawLines(particle: Particle):Unit = particle.neighbors.foreach { n =>
      line(particle.currentPos.x * 20 + 20, particle.currentPos.y * 20 + 20,
           cloth.grid(n.x)(n.y).currentPos.x * 20 + 20, cloth.grid(n.x)(n.y).currentPos.y* 20 + 20)
    }

    for(column <- cloth.grid; particle <- column) drawLines(particle)
    cloth.verletIntegration
    cloth.satisfyConstraints
  }
}

Position.scala:

package cloth

case class Position(x: Float, y: Float) {
    def -(that:Position) = Position(this.x - that.x, this.y - that.y)
    def +(that:Position) = Position(this.x + that.x, this.y + that.y)
    def *(scalar:Float) = Position(this.x * scalar, this.y * scalar)
    def length = math.sqrt(x*x + y*y).toFloat
}

Particle.scala:

package cloth

class Particle(
    var currentPos: Position,
    var previousPos: Position,
    val gridIndex: Coordinate,
    val restLength: Float,
    val neighbors: Array[Coordinate],
    var stuck: Boolean
    ) {

  val forces = 0.0f
}

... and Cloth.scala:

package cloth

import scala.collection

case class Coordinate(x: Int, y: Int)

class Cloth(
  val rows: Int,
  val columns: Int,
  var gravity: Float,
  val timestep: Float,
  val fixedParticles: List[Coordinate]
) {

  val grid = new Array[Array[Particle]](rows, columns)

  def createGrid(): Unit = for (x <- 0 until rows; y <- 0 until columns) {
    val coord = Coordinate(x, y)
    val pos = Position(x, y)
    grid(x)(y) = new Particle(pos, pos, coord, 1.0f, findNeighbors(coord),
                              fixedParticles.contains(coord))
  }

  def findNeighbors(coord: Coordinate): Array[Coordinate] = Array(
    coord.copy(x = coord.x - 1),
    coord.copy(y = coord.y - 1),
    coord.copy(x = coord.x + 1),
    coord.copy(y = coord.y + 1)
  ).filter(isOccupied(_))

  def isOccupied(coord: Coordinate): Boolean =
    (0 <= coord.x && coord.x < rows &&
     0 <= coord.y && coord.y < columns)

  def verletIntegration(): Unit = for (row <- grid; p <- row)
    p.currentPos =
      if (p.stuck) p.previousPos else {
        val multiplyByTime = Position(p.forces * timestep * timestep, gravity * timestep * timestep)
        val together = multiplyByTime + p.currentPos - p.previousPos
        p.previousPos = p.currentPos
        p.currentPos + together
    }

  def satisfyConstraints() {

    def calculateConstraint(constraint: Coordinate, p: Particle) {
      val c2 = grid(constraint.x)(constraint.y).currentPos
      val c1 = p.currentPos
      val delta = c2 - c1
      val difference = 0.5f * (1.0f - 1.0f / delta.length)
      val dtemp = delta * difference
      p.currentPos = c1 + dtemp
      grid(constraint.x)(constraint.y).currentPos = c2 - dtemp
    }

    for(row <- grid; p <- row)
      if (p.stuck) p.currentPos = p.previousPos
      else p.neighbors.foreach(calculateConstraint(_, p))
  }

}

Hope that helps...


Implementing a HashTable in Objective-C I am trying to implement a hash table in Objective-C.  I did some testing and the implementation seems to work.  I am using an NSMutableArray to store linked lists of entries.  It also resizes when it gets almost full.  I am using an NSString for a key and any object as a value.  I store NSNull objects in an NSMutableArray to be able initialize it to a certain size.

The hash function I copied from the Java HashTable implementation:

#import "YZHashTable.h"
#import "Entry.h"

@interface YZHashTable()

@property (nonatomic,strong) NSMutableArray *tableArray;
@property (nonatomic) NSInteger elementCount;
@end

@implementation YZHashTable

const int HASHTABLECAPACITY=10;
const double HASHTABLELOADFACTOR = 0.75;

-(instancetype)init
{
    self  = [super init];
    if(self){
        [self resizeTableArray];
        _elementCount = 0;
    }

    return self;
}

-(void)resizeTableArray
{
    for(int i = 0; i < HASHTABLECAPACITY; i++){
        [self.tableArray addObject:[NSNull null]];
    }
}

-(NSMutableArray *)tableArray
{
    if(!_tableArray){
        _tableArray = [[NSMutableArray alloc]initWithCapacity:HASHTABLECAPACITY];
    }
    return _tableArray;
}

-(void)setElementCount:(NSInteger)elementCount
{
    if(elementCount < 0) return;
    _elementCount = elementCount;
}

-(void)addObjectForKey:(NSString *)key andValue:(id) value
{
    if(self.elementCount > [self.tableArray count] * HASHTABLELOADFACTOR){
        [self resizeTableArray];
    }
    NSUInteger keyArrayIndex = [self indexForHash:[key hash]] % [self.tableArray count];
    if([[self.tableArray objectAtIndex:keyArrayIndex] isKindOfClass:[NSNull class]]){
        [self.tableArray setObject:[[Entry alloc]initWithKey:key andValue:value] atIndexedSubscript:keyArrayIndex];
    }else{
        Entry *root = [self.tableArray objectAtIndex:keyArrayIndex];
        while(root.next && ![root.key isEqualToString:key]){
            root = root.next;
        }
        if([root.key isEqualToString:key]){
            root.value = value;
        }else{
            root.next = [[Entry alloc]initWithKey:key andValue:value];
        }
    }
    self.elementCount++;
}

-(id)getObjectForKey:(NSString  *)key
{
    id value = nil;

    NSUInteger keyArrayIndex = [self indexForHash:[key hash]] % [self.tableArray count];
    Entry *root = [self.tableArray objectAtIndex:keyArrayIndex];
    if(![root isKindOfClass:[NSNull class]]){
        while(![root.key isEqualToString:key] || root.next != nil){
            root = root.next;
        }
        if([root.key isEqualToString:key]) value = root.value;
    }

    return value;
}

-(id)removeObjectForKey:(NSString  *)key
{
    id value = nil;

    NSUInteger keyArrayIndex = [self indexForHash:[key hash]] % [self.tableArray count];
    Entry *root = [self.tableArray objectAtIndex:keyArrayIndex];
    if(![root isKindOfClass:[NSNull class]]){
        if([root.key isEqualToString:key]){
            if(!root.next){
                [self.tableArray setObject:[NSNull null] atIndexedSubscript:keyArrayIndex];
            }else{
                root = root.next;
            }
            self.elementCount--;
        }else{
            while(root.next && ![root.next.key isEqualToString:key]){
                root = root.next;
            }
            if([root.next.key isEqualToString:key]){
                root.next = root.next.next;
                self.elementCount--;
            }
        }

    }
    return value;
}

-(NSUInteger)count
{
    return self.elementCount;
}

-(NSUInteger)indexForHash:(NSUInteger)hash
{
    hash ^= (hash>>20)^(hash>>12);
    return hash ^ (hash >> 7) ^ (hash >> 4);
}

@end

Use NSHashTable from iOS 6+ SDK.
The hash table is modeled after NSSet with the following differences:

It can hold weak references to its members.
Its members may be copied on input or may use pointer identity for equality and hashing.
It can contain arbitrary pointers (its members are not constrained to being objects).

Example usage:

 NSHashTable *hashTable = [NSHashTable
 hashTableWithOptions:NSPointerFunctionsCopyIn];
 [hashTable addObject:@"foo"];
 [hashTable addObject:@"bar"];
 [hashTable addObject:@100];
 [hashTable removeObject:@"bar"];
 NSLog(@"Members: %@", [hashTable allObjects]);


Simple jQuery tooltip My tooltip works fine, but I need to simplify this code.

var genderSelect = {

  getGenderSelect: function () {
    if(this.val('Girl')){
      $(this).find('#girl-character').show();
      $(this).find('#boy-character').hide();
    }else if(this.val('Boy')){
      $(this).find('#boy-character').show();
      $(this).find('#girl-character').hide();
    }
  },

  init: function(){
    $('.control-label input[name="gender"]').change(
      $('#caracters').show();
      genderSelect.getGenderSelect();
  )};

};

genderSelect.init();

You can shorten the getGenderSelect function like this:

var genderSelect = {

  getGenderSelect: function () {
    $(this).find('#girl-character').toggle(this.val() == 'Girl');
    $(this).find('#boy-character').toggle(this.val() == 'Boy');
  },

  init: function(){
    $('.control-label input[name="gender"]').change(
      $('#caracters').show();
      genderSelect.getGenderSelect();
  )};

};

genderSelect.init();

The toggle function takes a boolean which determines whether to show or hide the  selected elements.


Building a Tree from a flat List This is my incoming data:

id      text    parent
--      ----    ------
0       0       NULL
1       1       0
11      11      1
2       2       0
21      21      2
211     211     21
22      22      2
3       3       0

Which I receive in a List<TreeNode>, where TreeNode is:

public class TreeNode
{
    public int id { get; set; }
    public string text { get; set; }
    public int? parent { get; set; }
}

To represent my tree,

0
|--1
|  |--11
|
|--2
|  |--21
|  |  |--211
|  |
|  |--22
|
|--3

I have this class:

public class Tree
{
    public int? id { get; set; }
    public string text { get; set; }
    public List<Tree> children { get; set; }
}

I build the tree from the flat list of nodes with this:

Tree tree = treeNodes.BuildTree();

This is my implementation of BuildTree:

public static Tree BuildTree(this List<TreeNode> nodes)
{
    // Create a NULL-root tree
    Tree root = new Tree();

    // Add nodes (sub-trees) to the tree
    foreach (TreeNode node in nodes)
    {
        // traverse tree, find the parent, add child
        root.TraverseAndAddNode(node);
    }

    // Return the NULL-root
    // If the tree has an actual root, it will be the first child of this root
    return root;
}

public static bool TraverseAndAddNode(this Tree root, TreeNode node)
{
    bool nodeAdded = false;

    // Check if the current root is the parent of the node to be added
    if (root.id == node.parent)
    {
        if (root.children == null)
            root.children = new List<Tree>();

        root.children.Add(new Tree()
        {
            id = node.id,
            text = node.text,
            children = null
        });

        nodeAdded = true;
    }
    // Check if the current root has children and recurse the search
    else if (root.children != null)
    {
        foreach (Tree tree in root.children)
        {
            nodeAdded = tree.TraverseAndAddNode(node);

            if (nodeAdded)
                break;
        }
    }
    // Node's parent is not in the current tree
    else
    {
        nodeAdded = false;
    }

    return nodeAdded;
}

I'm able to see a few problems here, such as the traversal code here is not efficient as it uses linear search, and the implementation would be better off done with a Dictionary, and also that I'm unnecessarily creating a NULL root for the lack of being able to dynamically identify the true root from the flat data.

How can I do better? Is there a way I can dynamically construct partial sub-trees as I parse the flat-data, and keep linking these partially constructed trees to the main tree and keep building on?

EDIT:
* Removed nullable id from TreeNode -- changed int? id to int id
* Updated incorrect original example showing 1.1 instead of 11

Added the following examples:

Case 1:
Multiple NULL parents (NULL root is acceptable):

id    text           parent            NULL
--    ----           ------            |
1     C:             NULL              |--C:
2     drivers        1                 |  |--drivers
3     oem            2        --->     |     |--oem
4     D:             NULL              |
5     data           2                 |--D:
6     E:             NULL              |  |--data
                                       |
                                       |--E:

Case 2:
Single NULL parent (NULL root is avoidable):

id    text           parent            NULL
--    ----           ------            |
0     My PC          NULL              |--My PC
1     C:             0                    |
2     drivers        1                    |--C:
3     oem            2        --->        |  |--drivers
4     D:             0                    |     |--oem
5     data           2                    |
6     E:             0                    |--D:
                                          |  |--data
                                          |
                                          |--E:

Well your provided example data can't be real, because I never ever saw an int? which could take 1.1 as value.

I will apply to the posted code the standard C# naming convention, meaning that properties are named using PascalCase casing.

That being said, lets take a look at the Tree class

public class Tree
{
    public int? id { get; set; }
    public string text { get; set; }
    public List<Tree> children { get; set; }
}

which seems to be just a TreeNode with children and without a parent. IMO this is too much. Just add your TreeNode to a List<TreeNode> Children property and you won't need the Tree class anymore.

By initializing the Children property at creation of the object, you will waste some space (memory) but you won't need to check if Children == null anymore.
If the root node (parent == null) won't be the first node in the collection then your method will fail.
Omitting braces although they might be optional is dangerous because this can lead to hidden and therefore hard to find bugs.

I would like to encourage you to always use them.
Using recursion to tackle this problem is a good idea.

Another approach

which assumes the TreeNode looks like so

public class TreeNode
{
    public int? Id { get; set; }
    public string Text { get; set; }
    public int? Parent { get; set; }
    public List<TreeNode> Children { get; set; }

    public TreeNode()
    {
        Children = new List<TreeNode>();
    }
}

where I would usually make the setters private and fill the properties at constructor level, but I will leave this for you to do.

By using FirstOrDefault() we can get the root node very easily and should remove it from the nodes collection. Let us introduce a method to do this

private static TreeNode RemoveRootNode(this List<TreeNode> nodes)
{
    if (nodes == null)
    {
        throw new NullReferenceException("nodes");
    }

    var root = nodes.FirstOrDefault(n => !n.Parent.HasValue);
    if (root != null)
    {
        nodes.Remove(root);
    }

    return root;
}

Now we don't need the root node to be the first item in the collection anymore.

Next we need the "main"  method which takes a List<TreeNode> as a parameter and returns a TreeNode which is the root node like so

public static TreeNode BuildTree(this List<TreeNode> nodes)
{
    var root = nodes.RemoveRootNode();
    if (root == null) { throw new ArgumentOutOfRangeException("nodes"); }
    return root.BuildTree(nodes);
}

Edit

Based on the changed example data, we now can have multiple TreeNode with Parent == null which makes the RemoveRootNode() method superfluous and will result in TreeNode buildTree(this List<TreeNode>) like so

public static TreeNode BuildTree(this List<TreeNode> nodes)
{
    if (nodes == null)
    {
        throw new ArgumentNullException("nodes");
    }
    return new TreeNode().BuildTree(nodes);
}

As we see there should be a BuildTree(this TreeNode, List<TreeNode>) method which will look like:

private static TreeNode BuildTree(this TreeNode root, List<TreeNode> nodes)
{
    if (nodes.Count == 0) { return root; }

    var children = root.FetchChildren(nodes).ToList();
    root.Children.AddRange(children);
    root.RemoveChildren(nodes);

    for (int i = 0; i < children.Count; i++)
    {
        children[i] = children[i].BuildTree(nodes);
        if (nodes.Count == 0) { break; }
    }

    return root;
}

which is self-explanatory. It fetches the children by using

public static IEnumerable<TreeNode> FetchChildren(this TreeNode root, List<TreeNode> nodes)
{
    return nodes.Where(n => n.Parent == root.Id);
}

and add them to the Children property of the root node and removes them from the nodes by

public static void RemoveChildren(this TreeNode root, List<TreeNode> nodes)
{
    foreach (var node in root.Children)
    {
        nodes.Remove(node);
    }
}

Then it iterates over the the children and calls itself recursively.