Entity Framework 7: migrations add Initial

It was changed command for initial migration

Old:
dnx ef . migration add Initial
dnx ef . migration apply

New:
dnx ef migrations add Initial
dnx ef database update
EF Core Migration details you can find here

Racklog: Kan, Ga and Roo Race

The same problem solved with more prolog style using racklog:

Three runners, Kan, Ga and Roo took part in a cross country race. Prior to the race,
four spectators from the audience, A, B, C, and D, made their prognoses, as follows:
A: Either Kan or Ga will win.
B: If Ga is the second, Roo will win.
C: If Ga is the third, Kan will not win.
D: Either Ga or Roo will be the second.

After the race, it turned out that all four statements were correct. In what order did the runners finish?
1. Kan, Ga, Roo 
2. Kan, Roo, Ga 
3. Roo, Ga, Kan 
4. Ga, Roo, Kan 
5. Impossible to determine



The Solution:

#lang racket
(require racklog)

(define %solve
  (%rel (f s t members)(
      (f s t)
     (%= members (list 'Kan 'Ga 'Roo) )
     (%member f members)
     (%member s members)
     (%member t members)
     (%and (%/== f s) (%/== f t) (%/== s t))
     (%or (%== f 'Kan) (%== f 'Ga)) ;prognose A - Either Kan or Ga will win
     (%if-then-else (%== s 'Ga) (%== f 'Roo) %true ) ;prognose B - If Ga is the second, Roo will win.
     (%if-then-else (%== t 'Ga) (%== s 'Kan) %true ) ;prognose C -  If Ga is the third, Kan will not win
     (%or (%== s 'Roo) (%== s 'Ga)) ;prognose D - Either Ga or Roo will be the second.
   )
  )
)

;find all possible solution
(%which(first second third)(%solve first second third))
(%more)


The Answer: 
'((first . Ga) (second . Roo) (third . Kan))
#f

Scheme: Kan, Ga and Roo Race

The Problem:

Three runners, Kan, Ga and Roo took part in a cross country race. Prior to the race,
four spectators from the audience, A, B, C, and D, made their prognoses, as follows:
A: Either Kan or Ga will win.
B: If Ga is the second, Roo will win.
C: If Ga is the third, Kan will not win.
D: Either Ga or Roo will be the second.

After the race, it turned out that all four statements were correct. In what order did the runners finish?
1. Kan, Ga, Roo 
2. Kan, Roo, Ga 
3. Roo, Ga, Kan 
4. Ga, Roo, Kan 
5. Impossible to determine

Every statement must be converted into an equivalent formula that is in CNF (conjunctive normal form), therefore all patterns of match are OR joined ( is a disjunction of simple terms) and matches are AND joined ( is a conjunction of statements).

The Solution:

(define (check-race-result x)
  (and
   ;prognose A - Either Kan or Ga will win
   (match x
     [(list 'Kan _ _) #t]
     [(list 'Ga _ _) #t]
     [_ #f]
    )

    ;prognose B - If Ga is the second, Roo will win.
    (match x
      [(list 'Roo _ _) #t]
      [(not (list  _ 'Ga _ )) #t]
      [_ #f]
    )
   
   ;prognose C -  If Ga is the third, Kan will not win
    (match x
      [(not (list 'Kan _ _)) #t]
      [(not (list _ _ 'Ga)) #t]
      [_ #f]
    )
   
   ;prognose D - Either Ga or Roo will be the second.
   (match x
     [(list _  'Ga _) #t]
     [(list _ 'Roo _) #t]
     [_ #f]
    )
  )
)

(filter check-race-result (permutations '(Kan Ga Roo)))

Google OAuth credentials don't persist across TB sessions

I am using latest Thunderbird with built in calendar and Provider for Google Calendar 1.0.4
It was the issue that Google OAuth credentials don't persist across TB sessions with constant OAuth popups, and lack of using the saved OAuth credentials. I have checked firewall, antivirus, smart security and so on. without success.

We never read manuals, oh! It was just necessary to enter email on "new calendar create" form!



1. Remove all google calendar
2. Create new calendar
3. Enter email into "pickup an existing session or enter your email address" input field
4. In following pop up window you should enter only password!

Enjoy!

VMware error: VMnetDHCP - No subnet declaration for VMnet

VMware Workstation 12 Player

I have rewritten as always the files vmnetdhcp.conf and vmnetnat.conf to keep infrastructure consistent and it appears error message in EventViewer "No subnet declaration for VMnet8 (192.168.113.1). Please write a subnet declaration for the network segment to which interface VMnet8 is attached."

I have found that vmnetdcp.exe looks for value in registry
HKLM\System\CurrentControlSet\services\VMnetDHCP\Parameters\VirtualEthernetSegments\1\HostIpAddress

The registry key was still containing old IP address and didn't allow to start DHCP service. You should change it to actual one.
Please note that address has reverse HEX notation. e.g.
192.168.1.1 is reversed to 1.1.168.192 and key value is 0101A8C0


Enjoy!

Scheme: Another Math Kangaroo Problem

with quick solution

Under the numbers {1, 4, 8, 10, 20, 25, 50} there are three, the product is 100. 
What is the sum of these three numbers?

Let's define combination function to find all possible sets of three numbers and shortcut for product.

(define (combinations k nlst)
  (cond ((zero? k)
         '(()))
        ((null? nlst)
         '())
        (else
         (append (map (lambda (k-1)
                        (cons (car nlst) k-1))
                      (combinations (- k 1) (cdr nlst)))
                 (combinations k (cdr nlst))))))

(define (product xlst)
  (foldl * 1 xlst))

(filter (lambda(x) (= (product x) 100)) (combinations 3 '(1 4 8 10 20 25 50 )))

Answer is 30 and set is {1, 4, 25}

Enjoy!

Scheme: Math Kangaroo Contest 2013



It was really nice question at Math Kangaroo Contest 2013
Four cars enter a roundabout at the same time, each one from a different
direction, as shown in the diagram. Each of the cars drives less than a full
round of the roundabout, and no two cars leave the roundabout in the
same direction. How many different ways are there for the cars to leave
the roundabout? 

I would like to introduce elegant solution in scheme
(let ([src '(A B C D)])
 (length ( filter-not
           (lambda(x)
             (ormap eq? x src))
           (permutations src)) )
  
)

So you can find correct answer for four cars (9) , five cars (44) and more

Enjoy!

[SOLVED]: OTRS keeps to reopen tickets


when OTRS reopens consistently the tickets you have several options to resolve an issue

1. Disable state change with following options

PostMasterFollowUpState='open' # to keep ticket open
PostMasterFollowUpStateClosed='closed' # to keep ticket as closed if it was already closed




2. Disable followup mails individual per queue in Queue-Admin management using FollowUp combo box.

It exists also undocumented feature you can set default value for combo in Config.pm file
# 1-possible, 2-reject, 3-create new ticket
$Self->{'AdminDefaultFollowUpID'} = "3";


Enjoy!

ForEach LINQ Extension

Crosspost from codeproject.com

Introduction

There are many "missing" extensions of LINQ to Objects. You can look them up with LINQ Extensions Library or MoreLinq library, but with this article I would like to focus solely on ForEach extension. I believe that ForEach extension is the most missed feature from LINQ to Objects. While other extensions are nice-to-have and sometimes you use them and most of the time you don't, the ForEach extension is essential because foreach statements are intrinsic to C# code.

ForEach extension consists of just a simple foreach statement. The extension iterates over the elements of the collection and perform an action on each one of them. However, the benefit of this code being wrapped in an extension is that it can be written in a Lambda notation and can be chained with other LINQ expressions. If you just need the straightforward ForEach extension, you can NuGet it from the libraries I mentioned before. What I want to do in this article is to extend it a little bit further to accommodate more complex user-case scenarios.

ForEach

Our starting point is the basic and most simple version of ForEach extension. You can also take a look at the code in LINQ Extensions Library's ForEach.

// ForEach
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    foreach (TSource item in source)
        action(item);

    return source;
}

// Foreach with index
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    foreach (TSource item in source)
        action(item, index++);

    return source;
}

These are the obvious extensions. The first extension performs an action on each item. The second extension also passes the item's index in the source collection.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

arr.ForEach(x => { Console.WriteLine("item " + x); });

arr.ForEach((x, index) => { Console.WriteLine("item " + x + ", index " + index); });

ForEach with previous items

Consider a situation in which you would like to iterate over the collection but would also like to have a reference to the previous item or the 2 previous items. Of course you can generalize this situation to any number of previous items but I think a real-life scenarios would beg no more than 2 items at most.

// Foreach with index and previous item
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    TSource previousItem = default(TSource);
    foreach (TSource item in source)
    {
        action(item, index++, previousItem);
        previousItem = item;
    }

    return source;
}

// Foreach with index and 2 previous items
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    TSource previousItem1 = default(TSource);
    TSource previousItem2 = default(TSource);
    foreach (TSource item in source)
    {
        action(item, index++, previousItem1, previousItem2);
        previousItem2 = previousItem1;
        previousItem1 = item;
    }

    return source;
}

When there are no previous items (start of the collection), the extension method pass along the default value of the source type.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

// ForEach with index and previous item
arr.ForEach((x, index, previousItem) =>
{
    Console.WriteLine("previous item " + previousItem);
});

// ForEach with index and 2 previous items
arr.ForEach((x, index, previousItem1, previousItem2) =>
{
    Console.WriteLine(
        "previous item #1 " + previousItem1 + 
        ", previous item #2 " + previousItem2
    );
});

Breakable ForEach

A foreach statement is a loop which can be break out of by using the break statement. I wanted to mimic this behavior by letting the extension method to know when to continue and when to stop applying the action on the remaining items in the collection. The implementation is done by returning a boolean from the action. true means continue, false means break from the loop.

// Breakable ForEach
public static IEnumerable ForEach(this IEnumerable source, 
    Func action)
{
    foreach (TSource item in source)
    {
        if (!action(item))
            break;
    }

    return source;
}

This time the action argument is not an Action delegate but rather a Func delegate with boolean as its returning type. Breaking from the foreach loop doesn't impact the LINQ chaining because the source collection is returned as is. There are more overload versions of this Breakable ForEach, one with an index and 2 more with previous items.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

arr.ForEach(x => 
{ 
    Console.WriteLine("item " + x); 

    if (x == 2)
        return false; // break
    return true; // continue
});

arr.ForEach((x, index) => 
{ 
    Console.WriteLine("item " + x + ", index " + index); 

    if (index == 2)
        return false; // break
    return true; // continue
});

ForEach with previous results

We already saw that we can pass to the action one or two previous items from the collection. What if wanted to pass not the item but a value that is returned from the previous action? Consider our array of integers. A running sum for a given item in the array would be the current item + the result calculation of the running sum for the previous item.

// ForEach with index and previous result
public static IEnumerable ForEach(this IEnumerable source, 
    Func action)
{
    int index = 0;
    TResult previousResult = default(TResult);
    foreach (TSource item in source)
        previousResult = action(item, index++, previousResult);

    return source;
}

The extension method requires both the type of the source collection and the type of the return value of the action.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

// ForEach with index and previous result
arr.ForEach((x, index, previousResult) =>
{
    int result = 10 * x;
    Console.WriteLine("result " + result + ", previous result " + previousResult);
    return result;
});

// Running sum
arr.ForEach((x, index, previousResult) =>
{
    int runningSum = previousResult + x;
    Console.Write(runningSum + " ");
    return runningSum;
});

Conclusion

I integrated this code with all my work projects and it has been helping me immensely. It's working seamlessly with any LINQ expression that I write. I hope it will help you too.

HOWTO copy only newer files and preserve structure of directories

Often we need to copy only recently modified files and keep directories in its original states.
To display just modified files we can use command
# find . -type f -mtime 0
where . (dot) means start search from current directory
-type f means find and display only files otherwise directories appear also in the list
-mtime 0 means modify time was within last 24 hours
It looks good so far? Let’s continue.
# find . -type f -mtime 0 | xargs tar -cf /path_to/archive.tar
found filenames will be piped to tar command. Hence archive.tar contains all freshly changed files.
if any filename contains whitespaces it can be correctly handled with small changes
# find . -type f -mtime 0 –print0 | xargs -0 tar -cf /path_to/archive.tar
And what if we need only updated files in the last 10 minutes? It is not a problem at all.
Replace  -mtime 0 with –mmin -10
# find . -type f -mmin -10 –print0 | xargs -0 tar -cf /path_to/archive.tar

Enjoy!



HOWTO: removing spam referrer from your wordpress blog

I have recently discovered the plugin which helps me to remove completely all annoying referral sites in analytics.  Familiar pattern of best seo, social buttons, get free traffic and so on appears in your analytics?



The solution is simple - "Spam Referrer Block" Plugin. It's available from wordpress.org, All settings are clearly defined.



Enjoy!

HOWTO: Repair Logitech M325 Mouse

FixIt says that you will find single screw under CE label. It isn't always true.