Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / pargraphs.rb
Last active December 27, 2015 08:19
Start_tags = {"*" => :emphasis, "{" => :footnote}
End_tags = {:emphasis => "*", :footnote => "}"}
text = "Normal text. *emphasised* {footnote} Test end."
mode = :unknown
paragraphs = text.each_char.reduce([]) do |mem, char|
if mode == :unknown || mode == :normal && Start_tags.keys.include?(char)
mode = Start_tags[char] || :normal
mem << {type: mode, content: mode == :normal ? char : ""}
elsif char == End_tags[mode]
mode = :unknown
@jonelf
jonelf / top10.rb
Last active December 27, 2015 21:09
# Outputs the top 10 most used commands.
# Usage:
# history | ruby top10.rb
require 'Set'
excluded_commands = Set.new ["ls", "cd ..", "irb", "exit"]
distribution = Hash.new(0)
ARGF.each do |line|
command = line.match(/[\s\d]*(\D.*)/)[1].strip
distribution[command] += 1 unless excluded_commands.include? command
@jonelf
jonelf / string.rb
Last active December 29, 2015 13:19
Testing the string operations mentioned in http://mortoray.com/2013/11/27/the-string-type-is-broken/
s = "lëon"
puts "Precomposed #{s}"
puts "Reverse #{s.reverse!}"
puts "First three #{s[0..2]}"
puts "Length: #{s.length.to_s}"
puts
s = "noe\u0308l"
puts "Decomposed #{s}"
puts "Reverse #{s.reverse!}"
puts "First three #{s[0..2]}"
@jonelf
jonelf / fizzbuzz.fs
Created November 27, 2013 16:21
FizzBuzz
[<EntryPoint>]
let main _=
for n in 1..100 do
let s = seq {
if n % 3 = 0 then yield "Fizz"
if n % 5 = 0 then yield "Buzz" }
if Seq.isEmpty s then printf "%d"n
printfn "%s"(s |> String.concat "")
0
@jonelf
jonelf / dot-product.clj
Created December 3, 2013 11:45
Correction of one of the code examples in Clojure: the JFDI language by Michael O. Church. The original code tested for empty on xs instead of xr and it also did a recursive call with (next xr) instead of (rest xr).
(defn dot-product [xs ys]
(loop [acc 0.0, xr xs, yr ys]
(if (or (empty? xr) (empty? yr))
acc
(recur (+ acc (* (first xr) (first yr)))
(rest xr)
(rest yr)))))
# Recursive
def dot_product(xs, ys, acc = 0)
if xs.empty? or ys.empty?
acc
else
dot_product(xs.drop(1), ys.drop(1), acc + xs.first * ys.first)
end
end
# More idiomatic Ruby
@jonelf
jonelf / weird.rb
Last active December 30, 2015 09:09
Tests if a number is weird.
class Fixnum
def weird?
divisors = (1..self/2+1).select{|n| self % n == 0 }
return false if self == 2 or divisors.reduce(:+) <= self
!(2..divisors.length-1).map{|m| divisors.combination(m).to_a }.flatten(1).any?{|a| a.reduce(:+) == self }
end
end
@jonelf
jonelf / gist:8558960
Created January 22, 2014 13:45
In C# there are more than one way to compare an object and a string. What's the output?
object str1 = "leaky abstraction";
string str2 = "leaky" + " abstraction";
object str3 = "leaky" + " " + "abstraction" + "";
string str4 = "leaky";
str4 += " abstraction";
Console.WriteLine(str1 == str2);
Console.WriteLine(str3 == str4);
Console.WriteLine((string)str3 == str4);
Console.WriteLine(str3.ToString() == str4);
Console.WriteLine(str3.Equals(str4));
@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;