Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / generic_sort_order.rb
Created February 3, 2014 21:08
Generic sort order.
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 August 29, 2015 14:02
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.
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 August 18, 2014 12:13
My first and probably last program written in Brainf*ck.
++++++++++[>+++++++>++++++++++>++++>+<<<<-]>++.>+.+++++.>--------.<-----.>+++++++++++++.<++++++++.------------.+++++++++++++.>------------.>.
@jonelf
jonelf / asyncAwaitTest
Last active August 29, 2015 14:05
async await test
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 September 4, 2014 08:54
Histogram of characters used in Google Application Passwords
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 September 17, 2014 07:34
Validera personnummer i XPATH
(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 August 29, 2015 14:10
Range cover example
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 August 29, 2015 14:10
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 January 2, 2015 01:24
Lorenz Attractor in Swift
// 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 {

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: