A server response written in node/express/mongodb: does this look right? This is my very very very first attempt to write anything meaningful in node. The client side is a simple form that has login, workspace, and simply saves them onto the database. It would have been a 5-liner in PHP!

My question really is: does this code look sane/good? I am trying to avoid using async.js for now, just because I want to get my hands dirty a little to start with.

A few notes:

I want to check if workspace and user names are taken first, but I want to check for BOTH in case they are taken
There is still a slight race condition here: what two users submit different usernames and same workspace names at the same time? OK it's unlikely, but one of them will end up failing (one of them will be workspace-less). How would you address it? Simply deleting the user just created? Or...?
There is a lot of indentation here. I know that it's a known issue with node... I cannot really use "return" as a trick to interrupt execution, because it's often used in an anonymous function context. So...
I sometimes go next("Bad error here"). I plan on writing an error handler in Connect to handle these (I will probably log things onto the database).

Here we go...

// Helper function. The javascript on the other side calls this with Ajax, and expects
// a hash with "response" (can be OK or ERRORS) and a "errors" vector
var serverResponse = function(res, errors){
  if(errors.length){
    res.json( { response: 'ERRORS', errors: errors } , 200)
    console.log("Sent ERROR");
  } else {
    res.json( { response: 'OK' } , 200)
    console.log("Sent OK");
  }
}

// The actual call
exports.nonApiRegister = function(req, res, next){

  // ***** UNINDENTED ON PURPOSE: artificial delay
  setTimeout(function(){
  // *****

  var errors = [];
  var Workspace = mongoose.model("Workspace");
  var User = mongoose.model("User");

  User.findOne( { login: req.body.login}, function(err, docs){

    // Log database error if it's there
    if(err){
      next("Database error fetching user"); // TODO: add error handler
    } else {
      // If the user exists, add it to the error vector BUT keep going
      if(docs){
        errors.push({ field:'login', message: 'Login name taken, sorry!' } );
      }
      Workspace.findOne({ name: req.body.workspace }, function(err, docs){
        if(err){
          next("Database error fetching workspace");
        } else {
          if(docs){
            errors.push( {field: "workspace", message: "Workspace taken, sorry!"} );
          }

          if(errors.length != 0){
            serverResponse(res, errors);
          } else {

            //
            // AT THIS POINT, UNLESS SOMETHING JUMPS ON US, both user and workspace are available
            //

            // User doesn't exist: create it
            var u = new User();
            u.login = req.body.login;
            u.password = req.body.password[0];
            u.workspaceIds = [];

            u.save( function(err) {
              if(err){
                next("Database error saving user");
              } else {
                var w = new Workspace();
                w.name = req.body.workspace;
                w.activeFlag = true;
                w.ownerUserId = u._id;
                w.countryId = null;
                w.save( function(err){
                  if(err){
                    next("Database error saving workspace. WATCH OUT: user " + u.login + " doesn't have a workspace!");
                  } else{
                    serverResponse(res, errors);
                  }
                })
              }
            })
          }
        }
      })
    }
  });

  //
  } , 500); // Artificial timeout
  //
}

Here are some simple tips.

1)

You can combine the two if conditions for if(docs) and if(errors.length != 0), since if docs is true, then errors has positive length.

Old Code:

if (docs) {
    errors.push({
        'field' : field,
        'message' : errorMessage
    });
}
if (errors.length) {
    errors.push({
        field : "workspace",
        message : "Workspace taken, sorry!"
    });
    serverResponse(res, errors);
    return;
}

New Code:

if (docs) {
    errors.push({
        field : "workspace",
        message : "Workspace taken, sorry!"
    });
    serverResponse(res, errors);
    return;
}

2)

For this sitation, it's best not to make multiple function calls to the same function.

Old Code:

var serverResponse = function(res, errors){
  if(errors.length){
    res.json( { response: 'ERRORS', errors: errors } , 200)
    console.log("Sent ERROR");
  } else {
    res.json( { response: 'OK' } , 200)
    console.log("Sent OK");
  }
}

New Code:

var serverResponse = function (res, errors) {
    var status = {};
    if (errors.length) {
        status.response = 'ERRORS';
        status.errors  = errors;
    } else {
        status.response = 'OK';
    }
    res.json( status, 200 );
    console.log( "SENT " + status.response );
}

3)

Pass warnings but not errors. Deal with errors immediately.

4)

Avoid having too many else conditions. Use a if guard to avoid this.

Old Code:

function a( err ){
    if( err ){
        throw new Error( err );
    }else{
        //... code
    }
}

New Code:

function a( err ){
    if( err ){
        throw new Error( err );
        return
    }
    //... code
}

*/

5)

Don't repeat yourself.

6)

Split up functions longer than 8 - 12 lines into smaller functions.

7)

if(errors.length != 0){ is the same as if(error.length){.

8)

Try to only have 1 nexted functions created in a single function. The setTimeout section has this problem.

9)

If it doesn't hurt the performance or security of the program then I would make User and Workspace global variables. Otherwise you could pass them where needed.

Final Code Revision

var User, Workspace;
var serverResponse = function (res, errors) {
    var status = {};
    if (errors.length) {
        status.response = 'ERRORS';
        status.errors  = errors;
    } else {
        status.response = 'OK';
    }
    res.json( status, 200 );
    console.log( "SENT " + status.response );
};
var getDatabaseRequestFunc = function (field, errorMessage, callback) {
    return function (err, docs) {
        if (err) {
            next("Database error fetching field: " + field);
            return;
        }
        if (docs) {
            var error = [{
                'field' : field,
                'message' : errorMessage
            }];
            serverResponse(res, error);
            return;
        }
        callback();
    };
};
var getUserSaveFunc = function (req, res, next, user) {
    return function (err) {
        if (err) {
            next("Database error saving user");
            return;
        }
        var w = new Workspace();
        w.name = req.body.workspace;
        w.activeFlag = true;
        w.ownerUserId = user._id;
        w.countryId = null;
        w.save(function (err) {
            if (err) {
                next("Database error saving workspace. WATCH OUT: user " + user.login + " doesn't have a workspace!");
                return;
            }
            serverResponse(res, []);
        });
    };
};
var registerWorkspace = function (req, res, next) {
    Workspace.findOne({
        name : req.body.workspace
    }, getDatabaseRequestFunc( "workspace", "Workspace taken, sorry!" , function(){
            var u = new User();
            u.login = req.body.login;
            u.password = req.body.password[0];
            u.workspaceIds = [];
            u.save(getUserSaveFunc(req, res, next, u));
        })
    );
};
var registerUserLogin = function (req, res, next) {
    User.findOne({
        login : req.body.login
    }, getDatabaseRequestFunc( "login", "Login name taken, sorry!" , function(){
            registerWorkspace(req, res, next);
        })
    );
};
exports.nonApiRegister = function (req, res, next) {
    setTimeout(function () {
        User = mongoose.model("User");
        Workspace = mongoose.model("Workspace");
        registerUserLogin(req, res, next);
    }, 500);
};


You can parse your grid using some very useful functions in kotlin-stdlib:

val numbers = grid.lines().map { line -> line.split(' ').map(String::toInt) }

Personally I prefer "row/column" instead of "y/x". Thinking "y" before "x" is unnatural.
You can always define your own methods too which can improve readability. e.g.:

fun Iterable<Int>.product(): Int = reduce(Int::times)

Instead of using Math.max you might check to see if the given product is greater than the currently known maximum product and then assign it if it is. This is minor but personally I prefer to avoid unnecessary assignments.
I wouldn't worry about trying to do all of this in one pass. Each directional slice has different row/column bounds so I think four separate loops is the clearest/cleanest.
You can remove some duplicated code when it comes to calculating the product and updating the maximum.

e.g.:

var product = 0

fun maximizeProduct(numbers: List<Int>) {
    with(numbers.product()) { if (this > product) product = this }
}

// Horizontal
for (y in 0 until numbers.size)
    for (x in 0 until numbers[y].size - 3)
        maximizeProduct(numbers[y].slice(x..x + 3))

// Vertical
for (y in 0 until numbers.size - 3)
    for (x in 0 until numbers[y].size)
        maximizeProduct(numbers.slice(y..y + 3).map { it[x] })

// Down Right
for (y in 0 until numbers.size - 3)
    for (x in 0 until numbers[y].size - 3)
        maximizeProduct(numbers.slice(y..y + 3).mapIndexed { i, it -> it[x + i] })

// Down Left
for (y in 0 until numbers.size - 3)
    for (x in 3 until numbers[y].size)
        maximizeProduct(numbers.slice(y..y + 3).mapIndexed { i, it -> it[x - i] })

println(product)


Constructing entities from API with foreign keys I'm loading three kinds of entities from an API (in csv format unfortunately). While the first two are fine, the third has foreign keys to both of them. What I'm currently doing is constructing the first two lists and then while constructing the third list I assign the entities for every item. This is how it looks:

var platforms = await Task.Run(() => parsed.Data.Select(p => CreatePlatform(p,stations,lines)).ToList());

private static Platform CreatePlatform(Dictionary<string, string> data, List<Station> stations, List<Line> lines)
    {
        if (data.Keys.Except(PlatformFields).Any())
            return null; // TODO log; invalid data format
        Platform platform = null;
        try
        {
            platform = new Platform
            {
                SteigId = Convert.ToInt32(data["STEIG_ID"]),
                Richtung = data["RICHTUNG"],
                Reihenfolge = Convert.ToInt32(data["REIHENFOLGE"]),
                RblNummer = string.IsNullOrEmpty(data["RBL_NUMMER"]) ? null : (int?) Convert.ToInt32(data["RBL_NUMMER"]),
                Bereich = string.IsNullOrEmpty(data["BEREICH"])? null : (int?) Convert.ToInt32(data["BEREICH"]),
                Steig = data["STEIG"],
                SteigWgs84Lat = Convert.ToDouble(data["STEIG_WGS84_LAT"]),
                SteigWgs84Lon = Convert.ToDouble(data["STEIG_WGS84_LON"]),
                Stand = data["STAND"],

                Station = stations.Single(s => s.HaltestellenId == Convert.ToInt32(data["FK_HALTESTELLEN_ID"])),
                Line = lines.Single(l => l.LinienId == Convert.ToInt32(data["FK_LINIEN_ID"]))

            };
        }
        catch (FormatException ex)
        {
            return null; // TODO log;
        }
        return platform;
    }

Stations and Lines are sets that have been constructed in the exact same way (only without fk's). Hirarchy-wise I want the Stations to include a list of plattforms and each platform has the corresponding line. I also get this using the current method as entity framework handles the dependencies.

My problem is that this method of runnig two selects for every Platform item takes multiple seconds and i'm using this data for input suggestions which i'd like to be available as soon as possible

Is there a more efficient way of assigning the correct dependencies?

If there's some important information missing please tell me

Using .Single() on an unsorted list has O(n) cost. If you use a data structure with constant time lookups e.g. a Dictionary<int, Station> and a Dictionary<int, Line> instead you should see a performance improvement using key lookup:

Station = stations[Convert.ToInt32(data["FK_HALTESTELLEN_ID"])],
Line = lines[Convert.ToInt32(data["FK_LINIEN_ID"])]

As you're using Single, I assumed that the dictionary will definitely contain the entry.

I don't know how you're creating your Lists but there is the Enumerable.ToDictionary method which could be useful:

var stationsLookup = stations.ToDictionary(s => s.HaltestellenId);


Using tweet as command to shut down Ubuntu I want to use Tweets sent out by a twitter account as a command to shut down Ubuntu. For this, I've written a Python script using tweepy, a Python library for Twitter. I scheduled the script to run half past the hour every hour (for eg at 5:30, 6:30, and so on) using crontab.

Here's the script:

import tweepy
import os
from datetime import datetime
p=None

api= tweepy.API()

for status in api.user_timeline('tweetneag4'):
    while p==None:    #so that it checks only the first tweet
        if status.text.count(str((datetime.now().hour)+1))< 2 and str((datetime.time.now().hour)+1)==10:
            p=1
            pass
        elif str((datetime.now().hour)+1) in status.text:
            print "System shut down in 20 mins."
            os.system('shutdown -h +20')
            p=1

Here is a sample tweet:

#loadshedding schedule in 10mins: 15:00-21:00 #group4 #nea

The twitter account (@tweetneag4) tweets an alert 30 mins and 10 mins before the power outage begins (here, the account tweets if and only if there's a power outage next hour). What I've tried to do is check at 5:30 whether it posts a tweet regarding 6 o'clock. I've done this by seeing if the tweet contains the string "6"(when checking at 5). When checking for ten o'clock, an additional complication arises because it may contain "10" in two scenarios- first as in 10 o'clock and second as in 10mins later. (I'm sure there's a better way of doing this)

If you see the twitter feed, things should be clearer. How could I improve so that the code become more elegant?

import tweepy
import os
from datetime import datetime
p=None

Use of single letter variable names is confusing, I have no idea what p is for.

api= tweepy.API()

Put spaces around your =

for status in api.user_timeline('tweetneag4'):
    while p==None:    #so that it checks only the first tweet

What are you thinking here? If p doesn't get set to a true value, you'll loop over the next block repeatedly in an infinite loop. If you only want the first tweet pass count = 1 to user_timeline

        if status.text.count(str((datetime.now().hour)+1))< 2 and str((datetime.time.now().hour)+1)==10:

            p=1

Setting flags is ugly. If you must set boolean flags use True and False not None and 1.

            pass
        elif str((datetime.now().hour)+1) in status.text:
            print "System shut down in 20 mins."
            os.system('shutdown -h +20')
            p=1

It seems to me that a much more straightforward way of doing this is to look at the time at which the status was posted. It seems to be available as status.created_at. Based on that you know that outage is either 30 or 10 minutes after that. So if it has been more then 30 minutes since that message, it must have already happened and we aren't concerned.

Something like this:

import tweepy
import os
from datetime import datetime, timedelta

api= tweepy.API()

status = api.user_timeline('tweetneag4', count=1)[0]
age = datetime.now() - status.created_at
if age < timedelta(minutes = 45):
    print "System shut down in 20 mins."
    os.system('shutdown -h +20')

NOTE: I get negative ages when I run this, I'm assuming that's time zones. Its also possible my logic is screwy.

If you really do need to get the actual time from the text, your current strategy of searching for numbers in the text is fragile. Instead you could do something like this:

# split the message up by whitespace, this makes it easy to grab the outage time
outage_time = status.split()[4]
# find everything up to the colon
hour_text, junk = outage_time.split(':')
# parse the actual hour
hour = int(hour_text)

Finally, for more complex examples using regular expressions to parse the text would be a good idea.


Musical graph algorithm, with an inlined lookup helper function I am working with bit strings in clojure. My question is less about performance and more about how one should go about it (in particular see the last inlined version)

The tonnetz-array represents an adjacency matrix.

(set! *warn-on-reflection* true)
(set! *unchecked-math* true)

(def ^:private ^ints tonnetz-array
  (int-array [952 1904 3808 3521 2947 1799 3598 3101 2107 119 238 476]))

get-tonnetz-connected can be thought of as a graph lookup.

(defn- get-tonnetz-connected
  "given packed binary notes representing a chord,
  returns packed notes that mark all notes that can be reached in
  the tonnetz graph. This function is used by tonnetz-connected? as
  a way of building a map / traversing a graph at a far faster
  rate than a graph search"
  [packed]
  (loop [mask (int 0)
         ^int bits packed]
    (if (= bits 0)
      mask
      (let [low-bit (Integer/lowestOneBit bits)]
        (recur
         (bit-or mask (aget ^ints tonnetz-array
                            ^int (Integer/numberOfTrailingZeros low-bit)))
         (bit-xor bits low-bit))))))

this is the main function.

(defn tonnetz-connected?
  "do the notes form a connected graph?"
  [packed]
  (let [low-bit (Integer/lowestOneBit packed)]
    (loop [mask (aget ^ints tonnetz-array
                      (Integer/numberOfTrailingZeros low-bit))
           unmatched (bit-xor packed low-bit)]
      (if (= 0 unmatched)
        true
        (let [matches (bit-and mask unmatched)
              masks (bit-or mask (get-tonnetz-connected matches))
              next-unmatched (bit-xor matches unmatched)]
          (if (= unmatched next-unmatched)
            false
            (recur masks next-unmatched)))))))

I have found that inlining has improved things a bit, and this thing is fast and it is fast enough, but I would like to understand how to properly get the most out of low level operations.

An inlined version where I have tried to make sure that ints are used all the way though.

(defn tonnetz-connected?-2
  "do the notes form a connected graph?"
  [packed]
  (let [low-bit (int (Integer/lowestOneBit (int packed)))]
    (loop [mask (int (aget ^ints tonnetz-array
                           (int (Integer/numberOfTrailingZeros low-bit))))
           unmatched (int (bit-xor (int packed) low-bit))]
      (if (= 0 unmatched)
        true
        (let [matches (int (bit-and mask unmatched))
              masks (loop [mask (int mask)
                           bits (int matches)]
                      (if (= bits (int 0))
                        mask
                        (let [low-bit (int (Integer/lowestOneBit bits))]
                          (recur
                           (int (bit-or mask (aget ^ints tonnetz-array
                                                   ^int (int (Integer/numberOfTrailingZeros low-bit)))))
                           (int (bit-xor bits low-bit))))))
              next-unmatched (int (bit-xor matches unmatched))]
          (if (= unmatched next-unmatched)
            false
            (recur (int masks) (int next-unmatched))))))))

A dirty test function

(defn testit [n f]
  (let [xs (repeatedly n #(+ 1 (rand-int 4094)))]
    (time (do (doall (map f xs)) nil))))

generally I am averaging (ran each about 10 times):

clj-music.core> (testit 100000 tonnetz-connected?)
"Elapsed time: 77.175712 msecs"

and for the inlined version

clj-music.core> (testit 100000 tonnetz-connected?-2)
"Elapsed time: 61.413721 msecs"

So the performance has improved, but now I just have (int ...) everywhere. This was mainly to silence warnings like:

core.clj:165 recur arg for primitive local: mask is not matching primitive, had: Object, needed: long
Auto-boxing loop arg: mask

Is this the correct way?

... I would like to understand how to properly get the most out of low
  level operations. ... Is this the correct way? ...

Up to a point.

There's little or nothing wrong with your get-tonnetz-connected and
tonnetz-connected? functions. I've tidied up the latter a bit.
Your tuned tonnetz-connected?-2 is a bit faster, but not much.
Pre-calculating the tonnetz-connected? values into a data
structure roughly doubles its speed.
Finally, we take a look at a way to derive the tonnetz graph from
first principles.

Functionality

Given a chord (set of notes/pitch-classes):

the get-tonnetz-connected function returns the fattest chord you
can reach from it in the tonnetz graph;
the tonnetz-connected? function tells you whether all its notes are
connected in the tonnetz graph.

Here's my revised tonnetz-connected?, which is essentially the same as yours:

(defn tonnetz-connected?
  "is the subgraph of the chord in the tonnetz graph connected?"
  [^long chord]
  (let [chord (int chord)]
    (loop [found (Integer/lowestOneBit chord)
           unmatched chord]
      (or (zero? unmatched)
          (let [newly-found (bit-and found unmatched)]
            (and (not (zero? newly-found))
              (recur (bit-or found (get-tonnetz-connected newly-found))
                     (bit-and-not unmatched newly-found))))))))

What is it doing?

It conducts a breadth-first search of the chord's subgraph of the tonnetz
graph, starting at an arbitrary note (in fact, the lowest one) of the
chord.
It keeps track of the found notes and of the unmatched ones:
those yet to be found.
It terminates

when there are no unmatched notes , returning true; otherwise
when there are no newly-found notes, returning false.

I've improved the algorithm a little, by

simplifying the setup of the loop;
abbreviating the logic by using the and and or macros, which you
can often bring into play when if forms return true or false
values; and
returning false as soon as there are no newly-found notes, saving
a  breadth-first-search step.

I've also changed a bit-xor to a bit-and-not. This makes no difference for this bit-field representation of the graph, but would be a lot faster if the equivalent adjacency-list representation were in play.

But the main thing is that the working of the algorithm is clearer (to me, at least), because the names tell me what's going on.

The only functional advantage this version possesses over yours is that it can handle the empty chord (denoted by 0), whereas yours blows up.

Performance

I used Criterium to measure performance:

(bench (tonnetz-connected? (rand-int 4096)))
Evaluation count : 684489240 in 60 samples of 11408154 calls.
             Execution time mean : 89.362030 ns
    Execution time std-deviation : 5.205238 ns

And for the tuned version;

(bench (tonnetz-connected?-2 (inc (rand-int 4095))))
Evaluation count : 666279660 in 60 samples of 11104661 calls.
             Execution time mean : 88.302147 ns
    Execution time std-deviation : 1.055889 ns

The untuned version is now just as fast as the tuned one. The tuned one's previous advantage of about 15% could probably be restored by changing its algorithm in the same way. I haven't troubled to do this, since there's a better way to speed things up:

Pre-calculating the tonnetz-connected? function.

We can pre-load the values of tonnetz-connected? into an array of booleans:

(def tonnetz-connected?-array
  (let [ans (->> 4096
                 range
                 (map tonnetz-connected?)
                 boolean-array)]
    (fn [^long chord] (aget ans chord))))

Measuring its performance ...

(bench (tonnetz-connected?-array (rand-int 4096)))
Evaluation count : 1202760240 in 60 samples of 20046004 calls.
             Execution time mean : 48.140865 ns
    Execution time std-deviation : 0.349284 ns

It's 45% faster than the tuned version.

Appendix: Deriving the Tonnetz graph

(This is not something you asked about, but I think it's worth doing.)

The Tonnetz graph just drops out of the sky as a series of magic numbers:

(def ^:private ^ints tonnetz-array
  (int-array [952 1904 3808 3521 2947 1799 3598 3101 2107 119 238 476]))

I've reverse-engineered the construction of this graph.

We start from the relative intervals that make up the tonnetz relation, which are the same for every note/pitch-class:

[3 4 5 7 8 9]

To turn these into a bit-mask, we use

(defn bits [bitlist]
  (reduce
    (fn [a i] (bit-or a (bit-shift-left 1 i)))
    0
    bitlist))

And, indeed

(bits [3 4 5 7 8 9])
=> 952

the mask for note 0.

For every semitone increment, we have to rotate the twelve-bit mask one bit to the left. A function that yields a function to rotate an n bit bit-field one to the left is

(defn rotate-bits-left-fn [n]
  (fn [bit-field]
    (if (bit-test bit-field (dec n))
      (-> bit-field (bit-shift-left 1) (bit-clear n) (bit-or 1))
      (-> bit-field (bit-shift-left 1)))))

If we apply this twelve times to 952 we get

(->> 952
     (iterate (rotate-bits-left-fn 12))
     (take 12))
=> (952 1904 3808 3521 2947 1799 3598 3101 2107 119 238 476)

... the tonnetz mapping we've seen already.

Putting it all together, we can derive your tonnetz-array thus:

(def ^:private ^ints tonnetz-array
  (->> [3 4 5 7 8 9]
       bits
       (iterate (rotate-bits-left-fn 12))
       (take 12)
       int-array))


Computing Scrabble score in Golang I think we all know the game of Scrabble and how popular it is as a simple programming challenge. Here's my attempt at computing a Scrabble score for a given word done in Go.

I must admit, I'm fairly new to Go (been learning it for a week or so). Having said that, I'd greatly appreciate all sorts of tips for improving not only the solution itself but also the style to be more Go-ish.

I've used golint and gofmt on this, but with gofmt I'm not sure I use it correctly, as it simply prints out the code to stdout.

There's a simple test suit that comes along with the solution.

// Package scrabble deals with proper word score counting
package scrabble

import "strings"

// countScore returns a score map per given word
func countScore() map[rune]int {
    var scoreMap = map[string]int{
    "aeioulnrst": 1,
    "dg":         2,
    "bcmp":       3,
    "fhvwy":      4,
    "k":          5,
    "jx":         8,
    "qz":         10,
    }
    var actualScore = make(map[rune]int)
    for letters, score := range scoreMap {
        for _, letter := range letters {
            actualScore[letter] = score
        }
    }
    return actualScore
}

// Score count Scrabble score
func Score(word string) (score int) {
    for _, char := range strings.ToLower(word) {
        score += countScore()[char]
    }
    return
}

As for the solution, I've noticed it's kinda slow. What's more, it takes  over 600 allocs/op. That's a lot for such a simple task. Why is that?

package scrabble

import "testing"

func TestScore(t *testing.T) {
    for _, test := range scrabbleScoreTests {
        if actual := Score(test.input); actual != test.expected {
            t.Errorf("Score(%q) expected %d, Actual %d", test.input, test.expected, actual)
        }
    }
}

func BenchmarkScore(b *testing.B) {
    for i := 0; i < b.N; i++ {
        for _, test := range scrabbleScoreTests {
            Score(test.input)
        }
    }
}

Test cases:

package scrabble

type scrabbleTest struct {
    input    string
    expected int
}

var scrabbleScoreTests = []scrabbleTest{
    {"a", 1},                           // lowercase letter
    {"A", 1},                           // uppercase letter
    {"f", 4},                           // valuable letter
    {"at", 2},                          // short word
    {"zoo", 12},                        // short, valuable word
    {"street", 6},                      // medium word
    {"quirky", 22},                     // medium, valuable word
    {"OxyphenButazone", 41},            // long, mixed-case word
    {"pinata", 8},                      // english-like word
    {"", 0},                            // empty input
    {"abcdefghijklmnopqrstuvwxyz", 87}, // entire alphabet available
}

I'd greatly appreciate all sorts of tips for improving not only the
  solution itself but also the style to be more Go-ish.

For a real-world code review, code should be correct, maintainable, reasonably efficient, and, most importantly, readable.

Writing code is a process of stepwise refinement.

Go was designed for simplicity, readability, and performance.

I ran your benchmark on your complicated code:

$ go version
go version devel +4d5bb9c609 Fri Dec 20 23:07:52 2019 +0000 linux/amd64
$

$ go test *.go -bench=. -benchmem -run=!
BenchmarkScore-4        9438        126800 ns/op   67275 B/op   512 allocs/op

It seems very slow.

I ran your benchmark on leaf bebop's complicated code:

$ go test *.go -bench=. -benchmem -run=!
BenchmarkScore-4     1263572           936 ns/op      16 B/op     2 allocs/op

It is much better, but still slow.

I ran your benchmark on my simpler, "Go-ish" code:

$ go test *.go -bench=. -benchmem -run=!
BenchmarkScore-4    16844323            69.5 ns/op     0 B/op     0 allocs/op

// Score returns the sum of the Scrabble tile points
// for the letters in a word.
func Score(word string) int {
    var score int
    for i := 0; i < len(word); i++ {
        score += int(points[word[i]])
    }
    return score
}

var points = [256]byte{
    'A': 1, 'a': 1,
        ...
    'Z': 10, 'z': 10,
}

My code is around 1,819 times faster than your code and it makes no allocations.

My code is around 13 times faster than leaf bebop's code and it makes no allocations.

An array is a simple, fast, random access data structure. The points array is constructed once at compile time.

scrabble.go:

/*
Package scrabble deals with proper word score counting

Scrabble Rules | Official Word Game Rules | Board Games
https://scrabble.hasbro.com/en-us/rules
*/

package scrabble

// Score returns the sum of the Scrabble tile points
// for the letters in a word.
func Score(word string) int {
    var score int
    for i := 0; i < len(word); i++ {
        score += int(points[word[i]])
    }
    return score
}

var points = [256]byte{
    ' ': 0, // blank
    'A': 1, 'a': 1,
    'E': 1, 'e': 1,
    'I': 1, 'i': 1,
    'O': 1, 'o': 1,
    'U': 1, 'u': 1,
    'L': 1, 'l': 1,
    'N': 1, 'n': 1,
    'R': 1, 'r': 1,
    'S': 1, 's': 1,
    'T': 1, 't': 1,
    'D': 2, 'd': 2,
    'G': 2, 'g': 2,
    'B': 3, 'b': 3,
    'C': 3, 'c': 3,
    'M': 3, 'm': 3,
    'P': 3, 'p': 3,
    'F': 4, 'f': 4,
    'H': 4, 'h': 4,
    'V': 4, 'v': 4,
    'W': 4, 'w': 4,
    'Y': 4, 'y': 4,
    'K': 5, 'k': 5,
    'J': 8, 'j': 8,
    'X': 8, 'x': 8,
    'Q': 10, 'q': 10,
    'Z': 10, 'z': 10,
}

  Program testing can be a very effective way to show the presence of
  bugs, but it is hopelessly inadequate for showing their absence.

  A convincing demonstration of correctness being impossible as long as
  the mechanism is regarded as a black box, our only hope lies in not
  regarding the mechanism as a black box.

  Edsger W. Dijkstra

Your tests don't test for control characters, punctuation, numbers, and non-ASCII characters.

Your complex code and data structures look like a black box to your tests.

Locate a definitive description of the Scrabble tile points and scoring system:

Scrabble Rules | Official Word Game Rules | Board Games

Encode the tile points system in readable, array form. Like the definition, uppercase and lowercase letters and their points are on the same line. The lines are in the same order as the definition. Carefully match the array to the definition and the definition to the array, left-to-right then right-to-left and top-to-bottom then bottom-to-top. The Go compiler will check that the keys are unique.

Consider some automated tests, which can also serve as regression tests.

For the points array, compute sum and count verification totals, listing the elements in the same order as the definition, matching array to definition and definition to array, right-to-left, and left-to right.

We are now be able to conclude, if not prove, that the points array is correct.

The Score function, by design is simple and readable.

// Score returns the sum of the Scrabble tile points
// for the letters in a word.
func Score(word string) int {
    var score int
    for i := 0; i < len(word); i++ {
        score += int(points[word[i]])
    }
    return score
}

For a comprehensive Score function test, use the definition of points encoding, with the tests in the same form as the definition for easy comparison.

Add the test written while developing the algorithm for the Score function. It includes special, unusual, and error cases.

peterso_test.go:

package scrabble

import (
    "strings"
    "testing"
)

/*
Scrabble Rules | Official Word Game Rules | Board Games
https://scrabble.hasbro.com/en-us/rules
    (0 point)-blank
    (1 point)-A, E, I, O, U, L, N, S, T, R
    (2 points)-D, G
    (3 points)-B, C, M, P
    (4 points)-F, H, V, W, Y
    (5 points)-K
    (8 points)- J, X
    (10 points)-Q, Z
*/

func TestScores(t *testing.T) {
    var (
        sum   = 2 * (0*1 + 1*10 + 2*2 + 3*4 + 4*5 + 5*1 + 8*2 + 10*2)
        count = int(('z' - 'a' + 1) + ('Z' - 'A' + 1))
    )

    as, ac := 0, 0 // all
    us, uc := 0, 0 // upper
    ls, lc := 0, 0 // lower
    for i := 0; i < len(points); i++ {
        score := int(points[i])
        if score != 0 {
            as += score
            ac++
            if 'A' <= i && i <= 'Z' {
                us += score
                uc++
            }
            if 'a' <= i && i <= 'z' {
                ls += score
                lc++
            }
        }
    }
    if as != sum {
        t.Errorf("all sum: got %d; want %d", as, sum)
    }
    if ac != count {
        t.Errorf("all count: got %d; want %d", ac, count)
    }
    if 2*ls != sum {
        t.Errorf("lower sum: got %d; want %d", 2*ls, sum)
    }
    if 2*lc != count {
        t.Errorf("lower count: got %d; want %d", 2*lc, count)
    }
    if 2*us != sum {
        t.Errorf("upper sum: got %d; want %d", 2*us, sum)
    }
    if 2*uc != count {
        t.Errorf("upper count: got %d; want %d", 2*uc, count)
    }
}

func TestScore2(t *testing.T) {
    var tests1 = []struct {
        word   string
        points int
    }{
        {" ", 0},          // (0 point)-blank
        {"AEIOULNSTR", 1}, // (1 point)-A, E, I, O, U, L, N, S, T, R
        {"DG", 2},         // (2 points)-D, G
        {"BCMP", 3},       // (3 points)-B, C, M, P
        {"FHVWY", 4},      // (4 points)-F, H, V, W, Y
        {"K", 5},          // (5 points)-K
        {"JX", 8},         // (8 points)- J, X
        {"QZ", 10},        // (10 points)-Q, Z
    }
    for _, tt := range tests1 {
        word := tt.word
        word += strings.ToUpper(tt.word)
        word += strings.ToLower(tt.word)
        got := Score(word)
        score := 3 * (len(tt.word) * tt.points)
        if got != score {
            t.Errorf("Score(%s) : got %d; want %d", word, got, score)
        }
    }

    var tests2 = []struct {
        word  string
        score int
    }{
        {"", 0},
        {"Go", 3},
        {"Scrabble", 14},
        {"09\t\r\n.?!", 0},
        {"Français", 10},
        {"羅生門", 0}, // Rashōmon
    }
    for _, tt := range tests2 {
        got := Score(tt.word)
        if got != tt.score {
            t.Errorf("Score(%s) : got %d; want %d", tt.word, got, tt.score)
        }
    }
}

  with gofmt I'm not sure I use it correctly, as it simply prints out
  the code to stdout.

For most purposes, use the go fmt command.

  Gofmt (reformat) package sources

Usage:

go fmt [-n] [-x] [packages]

  Fmt runs the command 'gofmt -l -w' on the packages named by the import
  paths. It prints the names of the files that are modified.


Calculate Levenshtein distance between two strings in Python I need a function that checks how different are two different strings. I chose the Levenshtein distance as a quick approach, and implemented this function:

from difflib import ndiff

def calculate_levenshtein_distance(str_1, str_2):
    """
        The Levenshtein distance is a string metric for measuring the difference between two sequences.
        It is calculated as the minimum number of single-character edits necessary to transform one string into another
    """
    distance = 0
    buffer_removed = buffer_added = 0
    for x in ndiff(str_1, str_2):
        code = x[0]
        # Code ? is ignored as it does not translate to any modification
        if code == ' ':
            distance += max(buffer_removed, buffer_added)
            buffer_removed = buffer_added = 0
        elif code == '-':
            buffer_removed += 1
        elif code == '+':
            buffer_added += 1
    distance += max(buffer_removed, buffer_added)
    return distance

Then calling it as:

similarity = 1 - calculate_levenshtein_distance(str_1, str_2) / max(len(str_1), len(str_2))

How sloppy/prone to errors is this code? How can it be improved?

There is a module available for exactly that calculation, python-Levenshtein. You can install it with pip install python-Levenshtein.

It is implemented in C, so is probably faster than anything you can come up with yourself.

from Levenshtein import distance as levenshtein_distance

According to the docstring conventions, your docstring should look like this, i.e. with the indentation aligned to the """ and the line length curtailed to 80 characters.

def calculate_levenshtein_distance(str_1, str_2):
    """
    The Levenshtein distance is a string metric for measuring the difference
    between two sequences.
    It is calculated as the minimum number of single-character edits necessary to
    transform one string into another.
    """
    ...


How to correctly use lock when adding/renewing cached data in asp.net cache? This is sort of a follow up question on https://stackoverflow.com/questions/754661/httpruntime-cache-best-practices/11431198 where a reply from frankadelic contains this quote and code sample from http://msdn.microsoft.com/en-us/magazine/cc500561.aspx:

  The problem is that if you've got a query that takes 30 seconds and you're executing the page every second, in the time it takes to populate the cache item, 29 other requests will come in, all of which will attempt to populate the cache item with their own queries to the database. To solve this problem, you can add a thread lock to stop the other page executions from requesting the data from the database.

// check for cached results
object cachedResults = ctx.Cache["PersonList"];
ArrayList results = new ArrayList();

if  (cachedResults == null)
{
  // lock this section of the code
  // while we populate the list
  lock(lockObject)
  {
    // only populate if list was not populated by
    // another thread while this thread was waiting
    if (cachedResults == null)
    {
      ...
    }
  }
}

In my opinion this code sample would have benefitted from showing the actual cache assign/get logic as well. My assumption also is that the cachedResults reference in the sample above should be a static reference, otherwise different threads wont access the same instance.

Is this correct and would the following be a correct implementation of a property where you want to lazy-load data through the cache?

private static object _someDataCacheLock = new object();
private static object _cachedResults;
public List<object> SomeData
{
    get
    {
        HttpContext ctx = HttpContext.Current;
        _cachedResults = ctx.Cache["SomeData"] as List<object>;

        if (_cachedResults == null)
        {
            // lock this section of the code
            // while we populate the list
            lock (_someDataCacheLock)
            {
                // only populate if list was not populated by
                // another thread while this thread was waiting
                if (_cachedResults== null)
                {
                    _cachedResults= GetSomeData(); //db access inside GetSomeData()

                    ctx.Cache.Insert("SomeData",
                            _cachedResults,
                            null, DateTime.Now.AddMinutes(10),
                            TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                }
            }
        }

        return _cachedResults;
    }
}

Ok, so I'm going ahead with this code instead, where I've added one row that in my opinion must have been missing from the MSDN sample... Any feedback on this?

private static object _someDataCacheLock = new object();
public List<object> SomeData
{
    get
    {
        HttpContext ctx = HttpContext.Current;
        List<object> cachedResults = ctx.Cache["SomeData"] as List<object>;

        if (cachedResults == null)
        {
            // lock this section of the code
            // while we populate the list
            lock (_someDataCacheLock)
            {
                //This row was missing in the MSDN sample, I believe...
                cachedResults = ctx.Cache["SomeData"] as List<object>;

                // only populate if list was not populated by
                // another thread while this thread was waiting
                if (cachedResults == null)
                {
                    cachedResults = GetSomeData(); //db access inside GetSomeData()

                    ctx.Cache.Insert("SomeData",
                            cachedResults,
                            null, DateTime.Now.AddMinutes(10),
                            TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                }
            }
        }

        return cachedResults;
    }
}

First, I think your code doesn't make much sense. The only reason why Cache can be useful is if you want the cached data to expire after some time or if the memory is low. But you're preventing the memory to be freed by using the field _cachedResults, which will hold the data even after they are removed from the cache. This field is of no use for you, _cachedResults should be a local variable instead.

Second, your code is not reliably thread-safe, because of the way you're assigning to _cachedResults. What could happen is if GetSomeData() creates the result using new, an uninitialized object could be first assigned to _cachedResults and only then would be the constructor called. Such reordering could happen, because it's safe from the point of view of a single thread.

Normally, you would solve this by using a volatile write. But in your case, simply using a local variable instead of a field will be enough.

For more details, see The Famous Double-Check Locking Technique in Chapter 29 of Jeffrey Richter's CLR via C#, or the Wikipedia article Double-checked locking.


Converting an SVG polygon path from relative to absolute coordinates This question was moved from StackOverflow as suggested by some users.

I developed a function that aims to (should) provide a simple and effective method to transform a polygon <path> from relative to absolute. The purpose of this function is to replace similar SnapSVG function from my code, since I'm not processing any other type of path commands.

I need suggestions on how improve the code. Here's the thingy:

// simple pathToAbsolute for polygons
// returns an array with ABSOLUTE points of the polygon path
var pathToAbsolute = function(p) {
  var np = p.match(/(m[^l]*|l[^(l|z)]*)/gi), // we extract the segments string from path string
      l = np.length, s, c, r, x = 0, y = 0;

  for (var i = 0; i<l; i++){ // here we start processing each segment string
    np[i] = np[i].trim(); // we trim the segment string
    c = np[i][0].trim(); r = new RegExp(c); // the path command
    np[i] = np[i].replace(/(^|[^,])\s*-/g, '$1,-') // fix the x,y 186.101-87.287 to -186.101,-87.287 separation
                 .replace(/[\s+\,|\s|\,]/g,'|') // replace any spacing and comma with another separator
                 .replace(r,'') // remove the path command from string
                 .split('|'); // split by new separator

    np[i][0] === '' && np[i].shift(); // I need a way to avoid this

    np[i][0] = parseFloat(np[i][0]); // make sure we do math with numbers
    np[i][1] = parseFloat(np[i][1]);

    if (i === 0) { x+=np[i][0]; y +=np[i][1]; } // for M, we only need it's values

    if (i>0){ // for other segments
      x= !!np[i-1] ? np[i-1][0] : np[0][0]; // if we have a previous point
      y= !!np[i-1] ? np[i-1][1] : np[0][1]; // let's grab its x,y values for relative points

      np[i][0] = c === 'l' ? np[i][0] + x : np[i][0]; // here we fix the value of any
      np[i][1] = c === 'l' ? np[i][1] + y : np[i][1]; // relative point
    }
  }
  return np; // in the end we return the newPoints array
}

// testing the function
var star = document.getElementById('star'), svg = star.parentNode,
    starPoints = pathToAbsolute(star.getAttribute('d'));

var newStar = document.createElementNS('http://www.w3.org/2000/svg','path');
newStar.setAttribute('d', 'M'+starPoints.join('L')+'z');
newStar.setAttribute('fill','green');
newStar.setAttribute('transform','translate(30,30)');
svg.appendChild(newStar);
<div style="width:200px">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800" >

   <path id="star"
         fill="#eee"
         d="M301.113,12.011l99.25,179.996l201.864,38.778L461.706,380.808 l25.508,203.958l-186.101-87.287L115.01,584.766l25.507-203.958L0,230.785l201.86-38.778L301.113,12.011z"
    ></path>

  </svg>
</div>

As you can see, the function works pretty much as expected, but in the comments I've put some points of concerns where I'm not sure it's accurate or generally applicable.

My most concern is the way to calculate the new values for relative points now becoming absolute. Also I need a way to avoid shifting via np[0].shift() any segment that is not properly processed before. Perhaps I can improve some RegExp, just I'm not completely sure.

So I am open to any other improvement suggestion you may have, so please jump in.

I need something that should work IE9+, Chrome 4+, Safari 5+, regardless of the fact that pathSegList or pathDataAPI is supported or not, just like the original functions from SnapSVG. No polyfills are acceptable.

Your suggested improved function must return same thingy: the array with absolute points, probably supporting H and V path commands. The array format is required for other functions that process the array for other uses.

Your variable names are overly terse. It's had to follow the logic when it's all to do with a two-letter variable that also happens to be a 2D array.

Basically, you seem intent writing it as densely as possible rather than making it readable. But minifying javascript is what minification tools are for.

It's also strange that you pass in a complete path string, but you get an array back. An array that's missing the commands, and where you manually have to prefix M and postfix z. Why not output a complete path?

Your test just assumes that all the commands (apart from the initial M) was a L command. Which, since that the only command your code can interpret, isn't too terrible. It'd still be neater to rewrite the path code itself, though, and return a string.

Adding the z smells especially buggy, as it closes the path even if the original path wasn't closed.

As for overall structure/approach, I'd use replace with a callback and a global pattern, rather than create a match object, and much around with its contents.

In other words, I'd do this:

function pathToAbsolute(relativePath) {
  var pattern = /([ml])\s*(-?\d*\.?\d+)\s*,\s*(-?\d*\.?\d+)/ig,
      offset = null;

  // Scan the string for an M/L command (case-insensitive) and its two
  // coordinates, replacing any lower-case (relative) command with an
  // absolute version
  return relativePath.replace(pattern, function (match, command, x, y) {
    x = parseFloat(x);
    y = parseFloat(y);

    // If we don't have an offset yet or the command is absolute already,
    // then use the matched coordinates as the offset, and return the matched
    // string unaltered
    if(offset === null || command.toUpperCase() === command) {
      offset = { x: x, y: y };
      return match;
    }

    // If we're here, the command is relative, so add its coordinates to the
    // offset, and output the absolute version of the matched string
    offset.x += x;
    offset.y += y;

    return command.toUpperCase() + offset.x + "," + offset.y;
  });
}

// ========================
// Test code

var star = document.getElementById('star'),
    svg = star.parentNode,
    absPath = pathToAbsolute(star.getAttribute('d'));

var newStar = document.createElementNS('http://www.w3.org/2000/svg','path');
newStar.setAttribute('d', absPath);
newStar.setAttribute('fill','green');
newStar.setAttribute('transform','translate(30,30)');
svg.appendChild(newStar);
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800">
   <path id="star" fill="#eee" d="M301.113,12.011l99.25,179.996l201.864,38.778L461.706,380.808 l25.508,203.958l-186.101-87.287L115.01,584.766l25.507-203.958L0,230.785l201.86-38.778L301.113,12.011z"></path>
</svg>

Like yours it only looks for M and L commands - not any of the many other SVG commands. Though it shouldn't actually be too terrible to extend it to handle those as well. Especially, as this returns a complete path string that can be used right away. Not joining necessary, and no assuming all commands are Ls.

It does however still assume that the path is valid; that it starts with an m or M. So it's gigo: Garbage in, garbage out.

Edit: Although I do feel the code above answers the question as originally stated, here's a version that doesn't "transform a path", but parses it and returns an array.

function pathToAbsolute(relativePath) {
  var pattern = /([ml])\s*(-?\d*\.?\d+)\s*,\s*(-?\d*\.?\d+)/ig,
      coords = [];

  relativePath.replace(pattern, function (match, command, x, y) {
    var prev;

    x = parseFloat(x);
    y = parseFloat(y);

    if(coords.length === 0 || command.toUpperCase() === command) {
      coords.push([x, y]);
    } else {
      prev = coords[coords.length-1];
      coords.push([x + prev[0], y + prev[1]]);
    }
  });

  return coords;
}


A C++ PEG parser generator I wrote a C++ PEG packrat parser generator and would love to get some feedback on the code and/or syntax. I currently use the << operator for defining grammar expressions and linking the handler functions that are called when a rule is matched. These functions can then store a value of an arbitrary C++ type in the corresponding node of the parse tree using the value<type>() function.

For example, a very simple math parser can be generated through the following source code:

#include "IO.h"
#include "parserTools.h"
#include <cmath> //pow

using namespace llib;

std::map<std::string, double> variables;

void op(parser::info &lhs,parser::info &rhs,parser::info &r,const std::string &op){
  if(op[0]=='+')      r.value() =     lhs.value<double>() + rhs.value<double>() ;
  else if(op[0]=='-') r.value() =     lhs.value<double>() - rhs.value<double>() ;
  else if(op[0]=='*') r.value() =     lhs.value<double>() * rhs.value<double>() ;
  else if(op[0]=='/') r.value() =     lhs.value<double>() / rhs.value<double>() ;
  else if(op[0]=='^') r.value() = pow(lhs.value<double>() , rhs.value<double>());
}

void variable(parser::info &s){ s.value<double>()=variables[s.string()];             }
void negate(parser::info &s)  { s.value()=-s[0].value<double>();                     }
void set(parser::info &s)     { variables[s[0].string()]=s[1].value<double>();       }

int main(){
  peg::grammar math("Math");

  math << "Math"        << "Set | Expression"                           << parserTools::show("%v0\n");
  math << "Set"         << "Variable '=' Sum"                           << set << parserTools::pass(1);
  math << "Expression"  << "Sum"                                        << parserTools::pass();
  math << "Sum"         << "Product  ([+-] Product )*"                  << parserTools::leftbinary(op);
  math << "Product"     << "Exponent ([*/] Exponent)*"                  << parserTools::leftbinary(op);
  math << "Exponent"    << "Atomic   ('^'  Atomic  )*"                  << parserTools::leftbinary(op);
  math << "Atomic"      << "Parentheses | Number | Negate | Variable"   << parserTools::pass();
  math << "Parentheses" << "'(' Expression ')'"                         << parserTools::pass();
  math << "Negate"      << "'-' Atomic"                                 << negate;
  math << "Variable"    << "[a-zA-Z] [a-zA-Z0-9]*"                      << variable;
  math << "Number"      << parserTools::doubleGrammar()                 << parserTools::pass();

  math.ignored=" \t";
  math.setStart("Math");

  parser p(math);

  p.error=parserTools::genericError;
  p.warning=parserTools::genericWarning;

  while (1) {
    std::string str;
    IO << "> " >> str << str << " -> ";
    if(str=="q") break;
    else if(str!="") p.parse(str);
    else IO << clearline << endl;
  }

  return 0;
}

The full code and documentation is available on GitHub (though LParser is probably pretty boring name).

So any thoughts on the project? Is it something you would actually use?

void op(parser::info &lhs,parser::info &rhs,parser::info &r,const std::string &op){

I think I'd prefer a more descriptive name than r here. I'm guessing it's intended to stand for "result", in which case I think I'd prefer to just name it result.

  peg::grammar math("Math");

  math << "Math" << "Set | Expression" << parserTools::show("%v0\n");

It's not immediately clear why you specify Math both when you create the grammar object and when you specify rules. I think the second is naming the top-level rule Math, but it's not clear what the parameter to the constructor accomplishes.

  math << "Set" << "Variable '=' Sum" << set << parserTools::pass(1);

At least without some documentation on your parserTools, the meaning of the pass(1) isn't immediately obvious. Presumably this is (at least roughly equivalent to) a semantic rule that's executed when the associated pattern is matched. I can think of a couple of different things pass(1) might be intended to mean, but lac, context to even guess at which is likely to be accurate. Sufficient study of your documentation would undoubtedly make the meaning clear, but it wasn't immediately obvious.

This is kind of a difficult situation: you're basically defining an EDSL, which means understanding the code pretty much requires learning the language you've defined. In other words, no matter how carefully or well written the code is, it's likely to be at least somewhat difficult to understand in isolation.

Summary: looking at the code in isolation, the grammar rules are highly readable, but the (presumed) associated actions much less so.

math << "Exponent" << "Atomic   ('^'  Atomic  )*" << parserTools::leftbinary(op);

At first glance, I'd have assumed that leftbinary was intended to signify a left-associative binary operator--but exponentiation is normally right-associative. I'm left uncertain whether:

it's a bug,
you intentionally changed it from right- to left-associative, or
I've completely misinterpreted what leftbinary really means.

  while (1) {
    std::string str;
    IO << "> " >> str << str << " -> ";

I have mixed feelings about this mixture of input and output in the same expression. It does lead to relatively terse code, but requires unusual care in reading to keep track of which parts are input vs. output.

Ultimately, I think it would probably be better to review the parser generator and the IO library separately. Here I'm really concentrating on the parser generator, but the design if the I/O library might well be worth examination and review in its own right.


Improvements, TicTacToe in Scala I've just started my journey into functional programming and tried to implement a TicTacToe game Recursively without State in Scala.
What I dislike is the JOptionPane but I don't know if i can solve this more elegantly.
Does anyone know if i can solve this in a more "Scala" like way?

import javax.swing.JOptionPane

import scala.annotation.tailrec

class TicTacToe {
}

object TicTacToe {
  val l1: List[Int] = List(0, 1, 2)
  val l2: List[Int] = List(3, 4, 5)
  val l3: List[Int] = List(6, 7, 8)
  val c1: List[Int] = List(0, 3, 6)
  val c2: List[Int] = List(1, 4, 7)
  val c3: List[Int] = List(2, 5, 8)
  val d1: List[Int] = List(0, 4, 8)
  val d2: List[Int] = List(2, 4, 6)
  val patterns: List[List[Int]] = List(l1, l2, l3, c1, c2, c3, d1, d2)
  val winConditions: List[String] = List("xxx", "ooo")
  val startBoard = Array('#', '#', '#', '#', '#', '#', '#', '#', '#')

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

  def startGame: Unit ={
    @tailrec
    def playGameAt(game: List[Array[Char]], atPosition: Int): Unit = {
      val board: Array[Char] = game.head
      board.update(atPosition, nextPlayer(board))
      printBoard(board)
      if (!isWon(board)) {
        playGameAt(List(board) ++ game, Integer.parseInt(JOptionPane.showInputDialog()))
      }
    }
    playGameAt(List(startBoard), Integer.parseInt(JOptionPane.showInputDialog()))
  }

  def nextPlayer(board: Array[Char]): Char = {
    val turnNumber = board.count(_ == '#')
    if(turnNumber%2 == 0) 'x' else 'o'
  }

  def isWon(board: Array[Char]): Boolean = {
    patterns.foreach(pattern=>{
      val rowValues = pattern.foldLeft("")(_+board(_))
      if (winConditions.contains(rowValues)){
        println("Winner is " + rowValues)
        return true
      }
    })
    false
  }

  def printBoard(board: Array[Char]): Unit = {
    List(l1, l2, l3).foreach(row => println("" + board(row(0)) + board(row(1)) + board(row(2))))
    println("------")
  }
}

New, Improoved but still imperfect version

Sorry for the ridiculously long answer. I've added an alternative (better) way of doing this at the very bottom.
Here are a few things you could improve:
class TicTacToe
There's no need for this class. You can get (probably) rid of it.
Spaces
Overall, your code is well-formatted, but there a few instances like these, where you missed spaces:
if(turnNumber%2 == 0)    |>   if (turnNumber % 2 == 0)
pattern=>{               |>   pattern => {
def startGame: Unit ={   |>   def startGame(): Unit = {

Type aliases
I don't know about you, but I like using type aliases because they help me remember what each type represents. It's also handy when you have to refactor your code, e.g., if you want to represent moves using tuples representing the row and column ((Int, Int)) or you want to make a Board class instead of just using an array of characters, you don't have to change your method signatures - they can still return and accept objects of type Player and Board.
type Player = Char
type Board = Array[Player]

Unnecessary braces and the main method
Instead of
def main(args: Array[String]): Unit = {
    startGame
}

you could make it a one-liner
def main(args: Array[String]) = startGame

However, it'd be much more helpful to announce instructions before starting the game (I know those instructions don't match your own game, but bear with me).
def main(args: Array[String]): Unit = {
    println("Welcome to Tic Tac Toe!")
    println("To play, enter the row and column of the cell where you want to move when prompted")
    println("Both the row and column must be numbers from 1 to 3")

    runGame()
}

startGame
Rather than startGame, I feel like you should name it runGame, but that's entirely subjective, and you should pick whatever feels more intuitive to you.
More importantly, I think the startGame should be a nilary method rather than a nullary method, i.e., it should have an empty parameter list so that it looks like a proper method call rather than a property access. Currently, it looks very confusing when you just have startGame to run the entire game, since it looks like an unused expression.

Storing the board
Using a 1-D array of characters to represent a board is fine for now, although not very good functional programming style. There are a lot of other issues here, though.
board.update
You can use board(atPosition) = nextPlayer(board) instead of board.update(atPosition, nextPlayer(board)), since the update method is one of Scala's special methods that let you use syntactic sugar.
Adding the current board to the game
Currently, you use List(board) ++ game, which creates a new list and then concatenates game to it. A better approach would be board :: game, which is more idiomatic and simpler.
Why using a List[Array[Char]] is bad
First of all, there is absolutely no reason to maintain a list of all the past boards. You don't use the game variable everywhere. You can just have a single Array[Char] to keep track of the current board. Even if you do need to be able to go back to a previous move, you can just maintain a List[Move] and use it to reconstruct the board at a specific time.
Second, every single board you store in game refers to the exact same object! You're not copying board anywhere, so when you write the below code, you're getting the head of game, updating it, and then prepending that same board back onto game.
val board: Array[Char] = game.head
board.update(atPosition, nextPlayer(board))

A better solution would be to use tuples or a case class of your making. I'm going to just use tuples for now, because case classes would bloat the code.
type Row = (Player, Player, Player)
type Board = (Row, Row, Row)

Now that the board's 2-D, let's also make our moves 2-D, actually. They'll represent the row and column of each move. I made them 1-indexed in my code because I also make the user pick a number from 1 to 3 instead of 0 to 2 because I feel it'd be easier. By also internally using 1 instead of 0, we'll reduce off-by-one errors.
type Move = (Int, Int)

Variables
At the top of your object, you have a ton of variables, of which you need only one - patterns (which I'm going to rename to winCases, because that is more descriptive to me). There's no need to make separate public variables for l1, l2, etc. You can just do it like this (I used Set because the order doesn't matter):
val winCases = Set(
    Set((1, 1), (1, 2), (1, 3)),
    Set((2, 1), (2, 2), (2, 3)),
    Set((3, 1), (3, 2), (3, 3)),
    Set((1, 1), (2, 1), (3, 1)),
    Set((1, 2), (2, 2), (3, 2)),
    Set((1, 3), (2, 3), (3, 3)),
    Set((1, 1), (2, 2), (3, 3)),
    Set((1, 3), (2, 2), (3, 1)),
  )

startBoard can just be a local variable in runGame and doesn't have to be accessible by everyone.
winConditions we won't need because the whole isWon method can be refactored. There's absolutely no need to make a new string for each element of patterns/winCases.

Finding the winner
I don't like the fact that your isWon method prints the winner instead of only returning whether or not someone has won the game. A better way to do it would be to return a player and let the calling method decide what to display to the user. For that, I made this method:
def findWinner(board: Board): Either[Unit, Option[Player]] =
    if (isWinner(player1, board)) Right(Some(player1))
    else if (isWinner(player2, board)) Right(Some(player2))
    else if (isTie(board)) Right(None)
    else Left(())

Having an Either[Unit, Option[Player]] lets us encode multiple things into that single value. If it's a Left, we know the game will continue. If it's a Right, the game's ended. If it's the latter, containing a Some, there's a winner, and if it's a None, there's a tie.

Back to startGame/runGame
Among other things, I've renamed the playGameAt function to playRound. I also changed the signature. It takes the current board and player, and outputs the winner of the game. If there's a winner, it's a Some. If there's a tie, it's a None.
def playRound(board: Board, curr: Player): Option[Player]

Here's how the new function looks:
@tailrec
def playRound(board: Board, curr: Player): Option[Player] = {
  printBoard(board)
  println(s"Player $curr's move")
  val move = nextMove(board)
  val newBoard = moveTo(curr, move, board)
  findWinner(newBoard) match {
    case Right(possWinner) => possWinner
    case Left(_)           => playRound(newBoard, nextPlayer(curr))
  }
}

I've put the call to printBoard at the very top, because even though all tic-tac-toe boards are the same (unless you're playing a special variant), I personally would like to see the board I'm going to be moving on before I actually select a move. It's totally subjective, but I prefer it this way.
The print statement lets you know which player is supposed to move. The way you have it now, the users have to scroll up to see whose move it is currently, and this seems more helpful.
After that, it gets the next move with the nextMove function (which I'll put in later), creates a new board using that move, and tries to find the winner (see above for how the findWinner method works). If the game's ended, it returns the winner (or None if it's a tie). If not, it plays another round using a new board and the other player.
nextPlayer is implemented like this, by the way:
def nextPlayer(curr: Player): Player =
    if (curr == player1) player2
    else player1

The entire runGame function looks like this:
def runGame() = {
    @tailrec
    def playRound(board: Board, curr: Player): Option[Player] = ...

  val startBoard = (
    (default, default, default),
    (default, default, default),
    (default, default, default)
  )

  val winner = playRound(startBoard, player1)
  winner match {
    case Some(player) => println(s"Player $player won!")
    case None         => println("Tie")
  }
}

Here, startBoard is just a local variable, because I don't think there's any reason for anybody outside this method to know about it. default is a Char that represents a cell where no one's moved. player1 and player2 are used to mark where Player 1 and Player 2 moved, respectively.
val default = ' '
val player1 = 'x'
val player2 = 'o'

I also moved the print statement from isWon to here, so that there could be a customized message. Otherwise, if there was a tie, nothing would happen.

User input
It doesn't make sense to me that you're using a JOptionPane and displaying everything in the terminal. Why not make the user input also come from the console? We can write our nextMove method like this:
@tailrec
def nextMove(board: Board): Move = {
  val move = (nextRowOrCol("Row"), nextRowOrCol("Column"))

  if (isValid(move, board)) {
    move
  } else {
    println("That move is already taken. Please enter a different move.")
    nextMove(board)
  }
}

The above code reads a row and column using the helper function nextRowOrCol, then checks if it's a valid move, i.e., no one's moved there already. If it is, it just returns it, and if not, it gets the user to re-enter a move. I'll add the isValid function a little below.
nextRowOrCol is implemented like this (prompt can be either "Row" or "Column"). It uses regex to ensure that the input is a number between 1 and 3. Like the nextMove function, if the row or column inputted is valid, it returns it directly, otherwise, it prompts the user(s) again.
private def nextRowOrCol(prompt: String): Int = {
  val input = readLine(s"$prompt: ")

  if (input.matches("[1-3]")) {
    input.toInt
  } else {
    println("Please enter a number from 1 to 3")
    nextRowOrCol(prompt)
  }
}

The entire code
As the heading says, here's the entire code. You'll notice that it's wayyy longer than what you have currently, mostly because I used tuples, which complicated everything, but also because your original solution didn't have a lot of functionality.
I'm sure you can find a way to make it shorter, especially if you use your own case classes to represent everything. I've made almost every function that deals with tiny details like destructuring tuples private, but the public functions shouldn't need to be changed much even if you do decide to make a case class Board or something like that.
import scala.io.StdIn.readLine
import scala.annotation.tailrec

object TicTacToe {

  type Player = Char
  type Move = (Int, Int)
  type Row = (Player, Player, Player)
  type Board = (Row, Row, Row)

  val winCases = Set(
    Set((1, 1), (1, 2), (1, 3)),
    Set((2, 1), (2, 2), (2, 3)),
    Set((3, 1), (3, 2), (3, 3)),
    Set((1, 1), (2, 1), (3, 1)),
    Set((1, 2), (2, 2), (3, 2)),
    Set((1, 3), (2, 3), (3, 3)),
    Set((1, 1), (2, 2), (3, 3)),
    Set((1, 3), (2, 2), (3, 1)),
  )

  val default = ' '
  val player1 = 'x'
  val player2 = 'o'

  def main(args: Array[String]) = {
    println("Welcome to TicTacToe!")
    println("To play, enter the row and column of the cell where you want to move when prompted")
    println("Both the row and column must be numbers from 1 to 3")

    runGame()
  }

  def runGame() = {
    @tailrec
    def playRound(board: Board, curr: Player): Option[Player] = {
      printBoard(board)
      println(s"Player $curr's move")
      val move = nextMove(board)
      val newBoard = moveTo(curr, move, board)
      findWinner(newBoard) match {
        case Right(possWinner) => possWinner
        case Left(_)           => playRound(newBoard, nextPlayer(curr))
      }
    }

    val startBoard = (
      (default, default, default),
      (default, default, default),
      (default, default, default)
    )

    val winner = playRound(startBoard, player1)
    winner match {
      case Some(player) => println(s"Player $player won!")
      case None         => println("Tie")
    }
  }

  def findWinner(board: Board): Either[Unit, Option[Player]] =
    if (isWinner(player1, board)) Right(Some(player1))
    else if (isWinner(player2, board)) Right(Some(player2))
    else if (isTie(board)) Right(None)
    else Left(())

  def moveTo(player: Player, move: Move, board: Board): Board = {
    val (row0, row1, row2) = board
    val (r, c) = move

    def updateTuple[T](tup: (T, T, T), ind: Int)(f: T => T): (T, T, T) =
      ind match {
        case 1 => tup.copy(_1 = f(tup._1))
        case 2 => tup.copy(_2 = f(tup._2))
        case 3 => tup.copy(_3 = f(tup._3))
      }

    updateTuple(board, r) {
      row => updateTuple(row, c)(_ => player)
    }
  }

  def isWinner(player: Player, board: Board): Boolean =
    winCases.exists(winCase =>
      winCase.forall(move => playerAt(move, board) == player)
    )

  def isTie(board: Board): Boolean = !board.productIterator.exists {
    row => row.asInstanceOf[Row].productIterator.contains(default)
  }

  def playerAt(move: Move, board: Board): Player = {
    val (r, c) = move
    elementAt(elementAt(board, r), c)
  }

  private def elementAt[T](tup: (T, T, T), ind: Int): T =
    ind match {
      case 1 => tup._1
      case 2 => tup._2
      case 3 => tup._3
    }

  @tailrec
  def nextMove(board: Board): Move = {
    val move = (nextRowOrCol("Row"), nextRowOrCol("Column"))

    if (isValid(move, board)) {
      move
    } else {
      println("That move is already taken. Please enter a different move.")
      nextMove(board)
    }
  }

  private def nextRowOrCol(prompt: String): Int = {
    val input = readLine(s"$prompt: ")

    if (input.matches("[1-3]")) {
      input.toInt
    } else {
      println("Please enter a number from 1 to 3")
      nextRowOrCol(prompt)
    }
  }

  def isValid(move: Move, board: Board): Boolean =
    playerAt(move, board) == default

  def nextPlayer(curr: Player): Player =
    if (curr == player1) player2
    else player1

  def printBoard(board: Board): Unit =
    print(
      "__________________\n" +
      tup2String(
        mapTuple(board) {row => tup2String(row, "|")},
        "------\n"
      )
    )

  private def tup2String[T](tup: (T, T, T), sep: String): String =
    s"${tup._1}$sep${tup._2}$sep${tup._3}\n"

  private def mapTuple[T, R](tup: (T, T, T))(f: T => R): (R, R, R) =
    (f(tup._1), f(tup._2), f(tup._3))
}

An alternative way of storing the board
Working with tuples is really annoying, and even with case classes, you'd have to define your own methods and stuff. A nicer way to store the board would be to just maintain a list of all the moves anyone's ever made. Every move should contain where that move was made and the player who made that move, so let's makes these two types. By the way, the Move from before is more like Coord here.
type Coord = (Int, Int)
type Move = (Coord, Player)

Now everywhere we use board: Board, we just replace that with moves: List[Move].
playRound doesn't change all that much. The variable move now has to include the current player because of how we defined Move above, and the newMoves variable (analogous to newBoard) is constructed by prepending moves to the pre-existing list of moves, which is a lot easier than creating a moveTo function that does all sorts of crazy stuff inside. Everywhere else, just remember that board has been replaced with moves.
@tailrec
def playRound(moves: List[Move], curr: Player): Option[Player] = {
  println(s"Player $curr's move")
  val move = (nextMove(moves), curr)
  val newMoves = move :: moves
  printBoard(newMoves)
  findWinner(newMoves) match {
    case Right(possWinner) => possWinner
    case Left(_)           => playRound(newMoves, nextPlayer(curr))
  }
}

runGame only has 1 change: instead of manually creating a variable called startBoard filled with the default character (' '), you can use List.empty (or Nil):
val winner = playRound(List.empty, player1)

playerAt is a lot simpler now. It tries to find a move with the given coordinates, and if no move with those coordinates is found in our List[Move], then default is chosen.
def playerAt(coord: Coord, moves: List[Move]): Player =
    moves.find(move => move._1 == coord).map(_._2).getOrElse(default)

isTie is also a lot simpler - just check if 9 moves have been made!
def isTie(moves: List[Move]): Boolean = moves.size == 9

printBoard is the only one with big-ish changes (the good kind). You can just use map and mkString now that we're not using tuples.
def printBoard(moves: List[Move]): Unit =
    print(
      1 to 3 map { r =>
          1 to 3 map { c =>
            playerAt((r, c), moves)
          } mkString "|"
      } mkString ("__________\n", "\n------\n", "\n")
    )

The entire code:
import scala.io.StdIn.readLine
import scala.annotation.tailrec

object TicTacToe2 {

  type Player = Char
  type Coord = (Int, Int)
  type Move = (Coord, Player)

  val winCases: Set[Set[Coord]] = Set(
    Set((1, 1), (1, 2), (1, 3)),
    Set((2, 1), (2, 2), (2, 3)),
    Set((3, 1), (3, 2), (3, 3)),
    Set((1, 1), (2, 1), (3, 1)),
    Set((1, 2), (2, 2), (3, 2)),
    Set((1, 3), (2, 3), (3, 3)),
    Set((1, 1), (2, 2), (3, 3)),
    Set((1, 3), (2, 2), (3, 1))
  )

  val default = ' '
  val player1 = 'x'
  val player2 = 'o'

  def main(args: Array[String]) = {
    println("Welcome to TicTacToe!")
    println(
      "To play, enter the row and column of the cell where you want to move when prompted"
    )
    println("Both the row and column must be numbers from 1 to 3")
    printBoard(List.empty)

    runGame()
  }

  def runGame() = {
    @tailrec
    def playRound(moves: List[Move], curr: Player): Option[Player] = {
      println(s"Player $curr's move")
      val move = (nextMove(moves), curr)
      val newMoves = move :: moves
      printBoard(newMoves)
      findWinner(newMoves) match {
        case Right(possWinner) => possWinner
        case Left(_)           => playRound(newMoves, nextPlayer(curr))
      }
    }

    val winner = playRound(List.empty, player1)
    winner match {
      case Some(player) => println(s"Player $player won!")
      case None         => println("Tie")
    }
  }

  def findWinner(moves: List[Move]): Either[Unit, Option[Player]] =
    if (isWinner(player1, moves)) Right(Some(player1))
    else if (isWinner(player2, moves)) Right(Some(player2))
    else if (isTie(moves)) Right(None)
    else Left(())

  def isWinner(player: Player, moves: List[Move]): Boolean =
    winCases.exists { winCase =>
      winCase.forall(move => playerAt(move, moves) == player)
    }

  def isTie(moves: List[Move]): Boolean = moves.size == 9

  def playerAt(coord: Coord, moves: List[Move]): Player =
    moves.find(move => move._1 == coord).map(_._2).getOrElse(default)

  @tailrec
  def nextMove(moves: List[Move]): Coord = {
    val coord = (nextRowOrCol("Row"), nextRowOrCol("Column"))

    if (isValid(coord, moves)) {
      coord
    } else {
      println("That move is already taken. Please enter a different move.")
      nextMove(moves)
    }
  }

  private def nextRowOrCol(prompt: String): Int = {
    val input = readLine(s"$prompt: ")

    if (input.matches("[1-3]")) {
      input.toInt
    } else {
      println("Please enter a number from 1 to 3")
      nextRowOrCol(prompt)
    }
  }

  def isValid(coord: Coord, moves: List[Move]): Boolean =
    playerAt(coord, moves) == default

  def nextPlayer(curr: Player): Player =
    if (curr == player1) player2
    else player1

  def printBoard(moves: List[Move]): Unit =
    print(
      1 to 3 map { r =>
          1 to 3 map { c =>
            playerAt((r, c), moves)
          } mkString "|"
      } mkString ("__________\n", "\n------\n", "\n")
    )
}

By the way, here's a small change you can make concerning the isValid method. Instead of returning a boolean, return an Option with which you can do getOrElse. If you write a validate function like this:
def validate(coord: Coord, moves: List[Move]): Option[Coord] =
    Option.when(playerAt(coord, moves) == default)(coord)

you can use it in nextMove like this, which looks much more idiomatic. The only thing is that you'll have to drop the tailrec annotation.
def nextMove(moves: List[Move]): Coord = {
  val coord = (nextRowOrCol("Row"), nextRowOrCol("Column"))

  validate(coord, moves).getOrElse {
    println("That move is already taken. Please enter a different move.")
    nextMove(moves)
  }
}

Let me know if there's anything I did wrong or can improve.


Copy an Entity With Navigation Property in C# I have a table named Asset and each Asset has schedules. I create a method like this for copying an entity:

    public bool Copy(CopyAssetViewModel copyAssetViewModel,out string message)
    {
        var source = _equipments.AsNoTracking().FirstOrDefault(row => row.Id == copyAssetViewModel.AssetId);
        if (source == null) return false;
        int count = source.Schedules.Count;
        Asset newAsset = new Asset();
        Guid tempId = newAsset.Id;
        newAsset = source;
        newAsset.Id = tempId;
        newAsset.AssetCode = copyAssetViewModel.AssetCode;
        if (copyAssetViewModel.CopySchedules)
        {
            newAsset.Schedules = source.Schedules.ToList();
            newAsset.Schedules.ToList().ForEach(row =>
            {
                Schedule newSchedule = new Schedule();
                Guid scheduleId = newSchedule.Id;
                newSchedule = row;
                newSchedule.Id = scheduleId;
                newSchedule.AssetId = tempId;
            });
        }
        else
            newAsset.Schedules.Clear();
        _uow.MarkAsAdded(newAsset, _userManager.GetCurrentUserId());
        _uow.SaveChanges();
        return true;
    }

All entities inherit BaseEntity and I have a constructor for creating a new Id like this:

 public BaseEntity()
    {
        Id = SequentialGuidGenerator.NewSequentialGuid();
        CreatedOn = DateTime.Now;
    }

But when I use this code:

newAsset.Schedules = source.Schedules.ToList();

Id and createdOn are the same. I've gotten an error for a duplicate key. For solve this I used these lines:

 Schedule newSchedule = new Schedule();
                Guid scheduleId = newSchedule.Id;
                newSchedule = row;
                newSchedule.Id = scheduleId;

And I have another field that needs to update like: CreatedByUserId.

I think my code is very poor. Can everyone give me an idea to improve it?

copyAssetViewModel:

public class CopyAssetViewModel
{
    public Guid AssetId        { get; set; }
    public bool CopySchedules  { get; set; }
    public int AssetCode       { get; set; }
}

It appears that _equipments.AsNoTracking().FirstOrDefault gets some objects that have a unique ID

When you _uow.MarkAsAdded(newAsset, _userManager.GetCurrentUserId()); that collection requires unique ID. You go through round about way to to create a new ID so that you don't get a conflict.

Same thing for both Asset ID and Schedule ID.

All this is doing is assign a new unique ID.

Schedule newSchedule = new Schedule();
Guid scheduleId = newSchedule.Id;
newSchedule = row;
newSchedule.Id = scheduleId;

newSchedule is a reference to row so  newSchedule.Id = scheduleId; is just assigning a new ID to row

It someone is reading that different please comment.

It appears you changing code to get rid of error messages without fully understanding the code and objectives.


Munging data off the World Atlas of Language Structures website I have created a script to munge data off this tab-delimited table.

from collections import defaultdict
from utils import sync_and_read, currentdirectory

class MiniWALS(dict):
  def __init__(self, toupdate=True):
    WALS_URL = "http://wals.info/languoid.tab?sEcho=1&iSortingCols=1"+\
            "&iSortCol_0=0&sSortDir_0=asc"
    WALS_TXT = currentdirectory()+"/data/wals/wals.txt"

    wals_tsv = sync_and_read(WALS_URL, WALS_TXT, toupdate=toupdate)
    headerline, _ , data = wals_tsv.partition('\n')

    for line in data.split('\n'):
      lang = line.split()[0]
      for key, value in zip(headerline.split('\t')[1:], line.split('\t')[1:]):
        self.setdefault(lang,{})[key] = value

    self.GENUS = defaultdict(list)
    for lang in self:
      self.GENUS[self[lang]['genus']].append(lang)

    self.LANGUAGEFAMILY = defaultdict(list)
    for lang in self:
      self.LANGUAGEFAMILY[self[lang]['family']].append(lang)

    self.RELATED_LANGS = defaultdict(list)
    for lang in self:
      self.RELATED_LANGS[lang] = self.GENUS[self[lang]['genus']] + \
                                self.LANGUAGEFAMILY[self[lang]['family']]

The usage of the script is to access the WALS as such (JSON-like):

# Accessing WALS information
>>> wals = miniwals.MiniWALS()
>>> print wals['eng']
{u'glottocode': u'stan1293', u'name': u'English', u'family': u'Indo-European', u'longitude': u'0.0', u'sample 200': u'True', u'latitude': u'52.0', u'genus': u'Germanic', u'macroarea': u'Eurasia', u'sample 100': u'True'}

But there are cases where it's a little verbose, e.g:

for key, value in zip(headerline.split('\t')[1:], line.split('\t')

How code this script be improved?

There are a few efficiency improvements I can see:

Don't continually do an operation on data if that data doesn't change. Take your headerline variable. Each time you loop through your data, you do headerline.split('\t')[1:]. This is fairly inefficient since headerline never changes after you assign its value and thus you only need to do the operation once instead of \$n\$ times.

To fix this, save the result to a variable before your loop:

relevant_headers = headerline.split()[1:]
for line in data.split():
    . . .

Akin to the point above: don't do more work than you have to. You call split twice on line in your first for loop: first you split line and grab the language, then split it again to get the rest of the data. Instead of doing that, take advantage of unpacking in Python 3 or use pop() in Python 2:

# Python 3
language, *language_data = line.split()

# Python 2
language_data = line.split()
language = language_data.pop(0)

You can combine your final three for loops into one loop:

self.genus = defaultdict(list)
self.family = defaultdict(list)
self.related_languages = defaultdict(list)
for language, data in self.items():
    genus = self.genus[data['genus']]
    family = self.family[data['family']]

    genus.append(language)
    family.append(language)

    self.related_languages[language] = genus + family

Yes, we are reassigning self.related_languages[language] each iteration. However, this is an \$O(1)\$ operation, so it can be thought of as another basic operation. Since we now only have one for loop, only \$n\$ operations are done instead of \$3n\$. In terms of Big-O notation, the two versions perform the same; however, in terms of operation count and practical processing time, the above is better.

There are also a couple of style pointers I want to make:

Only use ALL_CAPS when dealing with constants. Because we cannot syntactically define constants in Python, convention is all we have to tell whether a variable is constant or not. So, unless a variable is actually supposed to be constant, do not use ALL_CAPS.

To go along with this point, when dealing with multiple words in a variable name, use underscores to separate the words:

# No
self.LANGUAGEFAMILY

# Yes
self.language_family

  NOTE: One exception to the rule ALL_CAPS rule is if you are using an acronym in a class name. So if WALS is an acronym, then MiniWALS is a slightly ambiguous, yet OK, name. Otherwise, I would suggest MiniWals.

Technically you indentation is fine. However, conventional Python uses 4 spaces to define code blocks.
Use format instead of string concatenation. This is in the style section because the performance benefits are debatable. However, using the format notation is much more readable and is more Pythonic:

# Instead of this...
WALS_TXT = currentdirectory()+"/data/wals/wals.txt"

# ... do this.
wals_text = '{}/data/wals/wals.txt'.format(currentdirectory())

Aside from these pointers and a few spacing issues and a not-needed +, your code stylistically looks fine.

Here is my version of your code:

from collections import defaultdict
from utils import sync_and_read, currentdirectory

class MiniWALS(dict):
    def __init__(self, toupdate=True):
        # Unless unfeasible, I always put instance variables at the top.
        self.genus = defaultdict(list)
        self.family = defaultdict(list)
        self.related_languages = defaultdict(list)

        wals_url = 'http://wals.info/languoid.tab?sEcho=1&iSortingCols=1'\
                   '&iSortCol_0=0&sSortDir_0=asc'
        wals_text = '{}/data/wals/wals.txt'.format(currentdirectory())

        wals_tsv = sync_and_read(wals_url, wals_text, toupdate=toupdate)
        headerline, _, data = wals_tsv.partition('\n')

        relevant_headers = headerline.split()[1:]
        for line in data.split():
            # I'm assuming Python 3.
            language, *language_data = line.split()
            for key, value in zip(relevant_headers, language_data):
                self.setdefault(language,{})[key] = value

        for language, data in self.items():
            genus = self.genus[data['genus']]
            family = self.family[data['family']]

            genus.append(language)
            family.append(language)

            self.related_languages[language] = genus + family


Removing from a collection in Excel VBA In my VBA code, I am trying to generate a specific list of cell row positions.

Basically, I first fill a collection with the entire list of row positions pertaining to a specific column:

Dim arrPos As New Collection
....
For i = 3 To bottomRow
arrPos.Add i
Next i

Then I try to remove remove values from this collection if there's no problem at that specific row.

For h = matchRow To 3 Step -1
    For g = arrPos.Count To 1 Step -1
        If CLng(Worksheets(".....").Range("C" & h).Value) = arrPos(g) Then
            arrPos.Remove (g)
            Exit For
        End If
    Next g
Next h

Basically Range("C" & h).Value is a column where the =MATCH function was used so there's a whole list row positions in that column. If the MATCH worked, then I can remove it from the collection. A similar type of loop is made use of further down the code for the rows where the MATCH came up false.

The code gives the proper results but it can drag on at times (especially since row counts can get up to the 5000's) and even crash my puny laptop. As you can see, the method makes use of nested loops and I believe that significant results could be attained by re-factoring this portion.

Does anyone have any suggestions?  The question is basically requesting a more efficient way to identify the row positions that did not come up from a MATCH function: either because the value was slightly erroneous or just simply missing.

The answer is to instead use VBA object Dictionary accessible by adding the "Microsoft Scripting Runtime" As a reference in your project. I don't know the internal mechanics but the dicitonary object has a method to see if an object .Exists within it's collection. This is much faster than my nested looping through an ordinary collection object and seeing if a specific value is contained.


RXJava operate on exiting List instead of creating new I'm dipping my toes in to RXJava for the first time. The function below works just fine but I can't help thinking it can be improved. For instance I'm sure that I'm unnecessarily creating a new List<Dish> sortedDishes when I could be performing operations on the exiting List<Dish> dishes that is passed in, I just don't know how yet.

public void sortDishes(int categoryId, List<Dish> dishes) {
    final List<Dish> sortedDishes = new ArrayList<>();
    final Integer[] subCategories = dbInteractor.getSubCategories(categoryId);
    final ArrayList<Integer> cats = new ArrayList<>();
    cats.add(categoryId);
    if (subCategories != null) {
        cats.addAll(Arrays.asList(subCategories));
    }

    Observable.from(dishes)
            .filter(new Func1<Dish, Boolean>() {
                @Override
                public Boolean call(Dish dish) {
                    Integer[] categories = categoryHelper.getCategorys(dish.getCategories());
                    for (Integer category : categories) {
                        if (cats.contains(category)) {
                            return true;
                        }
                    }
                    return false;
                }
            })
            .subscribe(new Subscriber<Dish>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Dish dish) {
                    sortedDishes.add(dish);
                }
            });
}

You're just abusing the library here. What you're doing can be done significantly more succinctly in standard java API:
public void sortDishes(int categoryId, List<Dish> dishes) {
    final Set<Integer> categories = new HashSet<Integer>();
    categories.add(categoryId);
    final Integer[] subCategories = dbInteractor.getSubCategories(categoryId);
    if (subCategories != null) {
        categories.addAll(Arrays.asList(subCategories));
    }

    final List<Dish> sortedDishes = dishes.stream()
        .filter(d -> categoryHelper.getCategories(d.getCategories())
                         .stream().anyMatch(categories::contains))
        .sort()
        .collect(Collectors.toList());
}

Note that this corrects a typo in getCategorys.

Further notes
Your dbInteractor's API could use a rework. I generally advocate against returning null from APIs that return a collection of elements, whether that's an Array or an actual Collection subclass.
I strongly suggest you consider returning new Integer[0] or even Collections.emptyList() because this will greatly simplify calling code by guaranteeing you're not returning null.
I also don't understand why the method is typed void. Either you actually modify the List you've received or you return a new List.


Determine if an int is within range Can I somehow make this a bit cleaner using Math.[Something], without making a method for it?

int MaxSpeed = 50;

if (Speed.X > MaxSpeed)
    Speed.X = MaxSpeed;

if (Speed.X < MaxSpeed * -1)
    Speed.X = MaxSpeed * -1;

if (Speed.Y > MaxSpeed)
    Speed.Y = MaxSpeed;

if (Speed.Y < MaxSpeed * -1)
    Speed.Y = MaxSpeed * -1;

There's a method in XNA called MathHelper.Clamp that does what you need. I understand if you don't want to import XNA libraries for this one function, though. You can do this with extension methods. Here's an implementation that uses generics (so you can apply it to various types) so you can use it for ints, longs, anything that implements IComparable

static class Extensions
{
    public static T Clamp<T> (this T self, T min, T max) where T: IComparable
    {
        if (self.CompareTo( min) <0)
            return min;
        return self.CompareTo(max) > 0 ? max : self;
    }
}


Storing a copy of unique_ptr When one writes c++ code to manipulate LinkedLists, Trees, etc. one creates a tmp pointer that points to the start/root and changes that pointer as one traverses along.
What would be the equivalent way of doing that in smart pointers?
For example, I wrote a Trie code using unique_ptrs to understand how to pass pointers around. To make the program work, I had to dive inside the smart pointer and access the underlying raw pointer. Is this the right way of doing things? Or is it infact possible to pass the unique_ptr around. I believe std::move will not be a good idea here since it will a destructive operation.
My code is as follows. Special call out to .get() op to get the raw pointer.
#include <iostream>
#include <memory>
#include <map>

class TrieNode
{
public:
    bool isLeaf{ false };
    std::map<char, std::unique_ptr<TrieNode>> children;
};

class Trie
{
    std::unique_ptr<TrieNode> root{nullptr};

public:
    Trie()
    {
        root.reset(new TrieNode());
    }

    void insert(const std::string& word)
    {
        auto node = root.get();
        for (auto ch : word)
        {
            if (node->children.find(ch) == node->children.end())
            {
                node->children[ch] = std::make_unique<TrieNode>();
            }
            node = node->children[ch].get();
        }
        node->isLeaf = true;
    }

    bool search(const std::string& word) const
    {
        auto node = root.get();
        for (auto ch : word)
        {
            if (node->children.find(ch) == node->children.end())
            {
                return false;
            }
            node = node->children[ch].get();
        }
        return (node && node->isLeaf);
    }
};

int main()
{
    Trie trie;
    trie.insert("application");
    std::cout << "does apple there in the trie? " << (trie.startsWith("app") ? "yes" : "no") << std::endl;
    std::cout << "is application there in the trie? " << (trie.search("apple") ? "yes" : "no") << std::endl;

    return 0;
}
```

The details of TrieNode don't need to be visible outside of Trie, so I recommend making it a private (or protected) member type Trie::Node.

We don't need to write a constructor, if we initialise the root node with the value we want:
std::unique_ptr<TrieNode> root = std::make_unique<TrieNode>();

Some coding styles ask us to indicate when auto resolves to a pointer type:
    auto* node = root.get();

Prefer passing a std::string_view than a const std::string& - it saves having to construct a string object in many circumstances (including the const char* string literals used in this program).  Either way, we're missing the required header file, and only succeed by dumb luck.

We're doing a lookup twice here:

        if (node->children.find(ch) == node->children.end())
        {
            node->children[ch] = std::make_unique<TrieNode>();
        }
        node = node->children[ch].get();

If we keep the result of find(), then we can use that iterator for that last line, instead of finding it all over again with [].  Perhaps a good idea to use insert() for adding the new node; that gives you back an iterator:
    auto* node = root.get();
    for (auto const ch: word) {
        auto& children = node->children;
        auto it = children.find(ch);
        if (it == children.end()) {
            it = children.emplace(ch, std::make_unique<TrieNode>()).first;
        }
        node = it->second.get();
    }

Similarly, in search():
bool search(const std::string_view word) const
{
    auto* node = root.get();
    for (auto const ch: word) {
        auto& children = node->children;
        auto it = children.find(ch);
        if (it == children.end()) {
            return false;
        }
        node = it->second.get();
    }
    return node->isLeaf;
}

This call:

trie.startsWith("app")

This fails to compile, as there's no such member.  It looks like part of the code is missing, which is certainly a sign that testing could be improved.

Don't flush streams unnecessarily using std::endl when a plain newline will do.

Improved program
#include <iostream>
#include <map>
#include <memory>
#include <string_view>

class Trie
{
    struct Node
    {
        bool isLeaf = false;
        std::map<char, std::unique_ptr<Node>> children = {};
    };

    std::unique_ptr<Node> root = std::make_unique<Node>();

public:
    void insert(const std::string_view word)
    {
        auto* node = root.get();
        for (auto const ch: word) {
            auto& children = node->children;
            auto it = children.find(ch);
            if (it == children.end()) {
                it = children.emplace(ch, std::make_unique<Node>()).first;
            }
            node = it->second.get();
        }
        node->isLeaf = true;
    }

    bool search(const std::string_view word) const
    {
        auto* node = root.get();
        for (auto const ch: word) {
            auto& children = node->children;
            auto it = children.find(ch);
            if (it == children.end()) {
                return false;
            }
            node = it->second.get();
        }
        return node->isLeaf;
    }
};

int main()
{
    Trie trie;
    trie.insert("application");

    for (auto const s: {"app", "apple", "application"}) {
        std::cout << s << (trie.search(s) ? " is present\n" : " not found\n");
    }
}