Skip to content

Instantly share code, notes, and snippets.

View markusl's full-sized avatar

Markus Lindqvist markusl

  • Supercell
  • Finland
View GitHub Profile
@markusl
markusl / FinnishSSN.fs
Created July 11, 2011 17:00
Finnish social security number calculator in F#
open System
/// Finnish social security number calculator in F#
/// Suomalainen henkilötunnuslaskuri F#:lla
module SSN =
let getSeparator year =
if year < 1900 then '+'
else if year < 2000 then '-'
else 'A'
@markusl
markusl / IpToCountrySlow.fs
Created July 12, 2011 17:12
Slow F# API for IpToCountry.csv - Class for mapping IP addresses to countries in FSharp using GPL'd CSV database from http://software77.net/geo-ip/
module IpToCountry
open System
open System.IO
/// Construct new class for mapping ip addresses to countries using
/// database from http://software77.net/geo-ip/
type IpToCountrySlow(?fileName) =
let fileName = defaultArg fileName "IpToCountry.csv"
// Read all non-comment lines
@markusl
markusl / IpToCountry.fs
Created July 13, 2011 15:35
Fast F# API for IpToCountry.csv - Class for mapping IP addresses to countries in FSharp using GPL'd CSV database from http://software77.net/geo-ip/
module IpToCountry
open System
open System.IO
type CountryCode = { code : string }
type IpAddressMapping = { cc : CountryCode; startAddress : uint32; }
/// Class to store the IP-addresses in 255 different buckets
/// countryIpList = The list of IP address mappings to store in this intance
@markusl
markusl / IpToCountrySlow.cpp
Created July 16, 2011 15:32
C++ class to map IP addresses to countries using database from http://software77.net/geo-ip/
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdexcept>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
@markusl
markusl / IpToCountry.cpp
Created July 17, 2011 13:45
Faster C++ class to map IP addresses to countries using database from http://software77.net/geo-ip/
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <array>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
@markusl
markusl / brainfuck.fs
Created August 11, 2011 17:31
Brainfuck interpreter written in F#
// FSharp (F#) interpreter for brainfuck programming language
// Read more from
// - http://www.muppetlabs.com/~breadbox/bf/
// - http://fsharp.net
// (c) 2011 Markus Lindqvist
module brainfuck
open System
let brainfuckMemorySize = 30000
@markusl
markusl / castvalue.fs
Created August 24, 2011 18:25
Implementing the Fast inverse square root (InvSqrt) in F#
#load "FsharpChart.fsx"
open MSDN.FSharp.Charting
let castValue (value : 'TSource) :'TResult =
let an = new System.Reflection.AssemblyName("CastUtil")
let ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(an, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave)
let mb = ab.DefineDynamicModule(an.Name)
let tb = mb.DefineType("CastUtil", System.Reflection.TypeAttributes.Public ||| System.Reflection.TypeAttributes.Class)
let fb = tb.DefineMethod("Read", System.Reflection.MethodAttributes.Public ||| System.Reflection.MethodAttributes.Static, typeof<'TResult>, ([|typeof<'TSource>|]))
let ig = fb.GetILGenerator()
@markusl
markusl / eoq.fs
Created October 28, 2011 12:26
Economic order quantity calculator in F#
/// Read more about EOQ from http://en.wikipedia.org/wiki/Economic_order_quantity
/// Using functional approach to handle user interface interaction using reactive programming style.
/// Any comments and improvements welcome :-)
/// -Markus L/2011
#if INTERACTIVE
#r "PresentationCore"
#r "PresentationFramework"
#r "System.Xaml"
@markusl
markusl / GCHQ_vm.fs
Last active September 28, 2015 20:17
GCHQ Challenge Stage 2
/// Implementation for GCHQ Challenge stage 2 virtual machine, in F#
/// From: http://canyoucrackit.co.uk/codeexplained.asp?v=1
/// This is a JavaScript programming challenge, with a cyber security angle. To solve
/// this stage an implementation of a simple virtual processor is required. Some notes
/// on the architecture are provided along with a block of data that can be analysed.
/// Solving this stage will reveal the final stage of the challenge.
/// The challenge originally suggested writing a virtual machine for the given instructions in JavaScript, but I decided to go with F#
/// See original 15b436de1f9107f3778aad525e5d0b20.js at http://pastebin.com/p5AHwPra
///
@markusl
markusl / CohenSutherland.fs
Created July 31, 2012 17:04
FSharp translation of the Cohen–Sutherland 2D line clipping algorithm
/// FSharp translation of the Cohen–Sutherland 2D line clipping algorithm
/// http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
///
/// Implemented in purely functional form which means there are no mutable variables.
/// No guarantees are given on the performance or reliability, it's not
/// thoroughly tested so feel free to submit unit tests. :-)
///
/// Markus Lindqvist 07/2012
module CohenSutherland