Skip to content

Instantly share code, notes, and snippets.

View tomlokhorst's full-sized avatar

Tom Lokhorst tomlokhorst

View GitHub Profile
@tomlokhorst
tomlokhorst / FuctorTest.cs
Created February 22, 2010 13:07
Functor type class ported to C#
// Functor type class ported to C#
// Only tested with: Mono C# compiler version 2.4.2.3
//
// Because .NET doesn't support higher kinded generics, we can't exactly port
// the Functor type class.
// However, if we're willing to give up a bit of type safety, we can get the
// IFuctor interface to work.
//
// We only have to add a single type cast, directly after using fmap. E.g:
// var xs = new List<int>();
@tomlokhorst
tomlokhorst / ReadTest.cs
Created February 23, 2010 18:14
Read type class ported to C#
// Read type class ported to C#
// Only tested with: Mono C# compiler version 2.4.2.3
//
// Because there are no static interface members, new instances of the
// Dictionary classes have to be created (although they contain no state).
//
// The functions using the read method (like mulStrings) explicitly mention
// `int` in IReadDictionary<int>, but that's just like `x = read "4" :: Int` in
// Haskell.
//
@tomlokhorst
tomlokhorst / MonadTest.cs
Created February 23, 2010 21:11
Monad type class ported to C#
// Monad type class ported to C#
// Only tested with: Mono C# compiler version 2.4.2.3
//
// The Monad type class has been split up into two .NET interface:
// - IMonad<A> has functions that can be applied _to_ monads (i.e. bind)
// - IMonadDictionary<A> has functions that _create_ monads (i.e. unit)
//
// Because .NET doesn't support higher kinded generics, we loose a bit of
// type safety. After calling `bind`, the return value has to be casted to
// the proper type.
{-# LANGUAGE DoRec #-}
module ExprLang where
import Control.Monad.Error
import Data.List
import Data.Maybe
-- Language
{-# LANGUAGE
TypeOperators
, TemplateHaskell
#-}
module Tie where
import Prelude hiding ((.), id)
import Control.Category
import Data.Record.Label
// Current (C# 5) syntax
var request = new CreateIndexRequest("new-index-name")
{
IndexSettings = new IndexSettings
{
Settings = new Dictionary<string, object>
{
{"index.settings", "value"}
},
Mappings = new List<RootObjectMapping>
@tomlokhorst
tomlokhorst / Optional+Unwrap.swift
Last active December 26, 2017 19:50
Unwrap multiple optionals in Swift 1.0
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
default:
return nil
}
}
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? {
@tomlokhorst
tomlokhorst / gist:6ed40e0e00cd2989a282
Last active August 29, 2015 14:08
SegueManager.swift
@tomlokhorst
tomlokhorst / gist:413457d8956d01638993
Created December 18, 2014 09:01
Don't zoom in on Google Maps when scrolling through a website
function fixMapScrollwheel(map) {
map.setOptions({ scrollwheel: false });
google.maps.event.addListener(map, 'click', function() {
map.setOptions({ scrollwheel: true });
});
google.maps.event.addListener(map, 'mouseout', function() {
map.setOptions({ scrollwheel: false });
});
@tomlokhorst
tomlokhorst / Promise.java
Last active August 29, 2015 14:16
Example of wrapping a RxJava Observable to get promise semantics.
// This is now available as a full library instead of a gist: https://github.com/Q42/RxPromise