Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonas Elfström jonelf

View GitHub Profile
View generic_sort_order.rb
sort_order = {"C" => 1, "Y" => 2, "M" => 3}
test_arr = ["M", "C", "Y"]
sorted = test_arr.sort_by {|c| sort_order[c]}
# => ["C", "Y", "M"]
@jonelf
jonelf / gist:47e3a1b2f06b66983e29
Last active Aug 29, 2015
Serializes POCO to XML. Converts it to Json with Json.NET. Parses it to a JObject and queries it with SelectToken. This turns an empty string to null.
View gist:47e3a1b2f06b66983e29
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
@jonelf
jonelf / gist:8147cdf385e8cd55ca02
Created Aug 18, 2014
My first and probably last program written in Brainf*ck.
View gist:8147cdf385e8cd55ca02
++++++++++[>+++++++>++++++++++>++++>+<<<<-]>++.>+.+++++.>--------.<-----.>+++++++++++++.<++++++++.------------.+++++++++++++.>------------.>.
@jonelf
jonelf / asyncAwaitTest
Last active Aug 29, 2015
async await test
View asyncAwaitTest
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public static void Main(string[] args)
@jonelf
jonelf / gist:4a5d9415776e823b50ac
Created Sep 4, 2014
Histogram of characters used in Google Application Passwords
View gist:4a5d9415776e823b50ac
def histogram(message)
message.each_char.reduce(Hash.new(0)) { |h, k| h[k] += 1; h}
end
def graph_histogram(h)
h.to_a.
sort_by{|kv| kv[1]*-1}.
map{|a| "#{a[0]} |#{('+'*a[1])[0..120]}#{a[1].to_s}\n"}.
join
end
@jonelf
jonelf / gist:188343b2070354a00513
Created Sep 17, 2014
Validera personnummer i XPATH
View gist:188343b2070354a00513
(validate-dateTime("yyyyMMdd", left($pnum, 8 )))
and
(((number(substring($pnum,3,1))*2) mod 9 + (floor(number(substring($pnum,3,1)) div 9)*9) + (number(substring($pnum,4,1))) +
(number(substring($pnum,5,1))*2) mod 9 + (floor(number(substring($pnum,5,1)) div 9)*9) + (number(substring($pnum,6,1))) +
(number(substring($pnum,7,1))*2) mod 9 + (floor(number(substring($pnum,7,1)) div 9)*9) + (number(substring($pnum,8,1))) +
(number(substring($pnum,9,1))*2) mod 9 + (floor(number(substring($pnum,9,1)) div 9)*9) + (number(substring($pnum,10,1))) +
(number(substring($pnum,11,1))*2) mod 9 + (floor(number(substring($pnum,11,1)) div 9)*9) + (number(substring($pnum,12,1)))
) mod 10 = 0)
@jonelf
jonelf / gist:223638855633144d9799
Last active Aug 29, 2015
Range cover example
View gist:223638855633144d9799
require 'nokogiri'
xml = %{
<Employments>
<Employment>
<From>2013-01-01</From>
<To>1900-01-01</To>
<MainEmployment>0</MainEmployment>
</Employment>
<Employment>
<From>2014-11-01</From>
@jonelf
jonelf / diamond
Last active Aug 29, 2015
Diamond
View diamond
first, last = "A".ord, "D".ord;
rows = (first...last).to_a + last.downto(first).to_a
cols = last.downto(first).to_a + (first + 1..last).to_a
rows.each do |row|
line = cols.map do |col|
col == row ? col.chr : "-"
end
puts line.join
@jonelf
jonelf / gist:965eed2ca03257c04335
Created Jan 2, 2015
Lorenz Attractor in Swift
View gist:965eed2ca03257c04335
// Swift Playground example of the Lorenz Attractor
// Change platform to OS X when opening Playground.
// alt + cmd + enter to show Assistant editor and see resulting image.
import Cocoa
import XCPlayground
let width = 600.0, height = 500.0
class CustomView: NSView {
View keybase.md

Keybase proof

I hereby claim:

  • I am jonelf on github.
  • I am jonelf (https://keybase.io/jonelf) on keybase.
  • I have a public key whose fingerprint is 6757 22B3 8909 BCA0 8CF3 2EEB D21C 666E 09FD 0C8B

To claim this, I am signing this object: