Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
bleis-tift / gist:2969500
Created June 22, 2012 00:32 — forked from forki/gist:2964839
Static duck typing (F#2.0)
let inline speak (a: ^a) =
let x = (^a : (member Name: string) (a))
printfn "I'm %s" x
let y = (^a : (member talk: unit -> string) (a))
printfn "I say %s" y
//type Duck =
// { Name : string } // Name is not property in F# 2.0
//
// member x.talk() = "quackity quack"
@tanakh
tanakh / gist:3017519
Created June 29, 2012 11:47
conduit-0.5, proxy server, simplified by using async-2.0
{-# LANGUAGE OverloadedStrings #-}
import Data.Conduit
import Data.Conduit.Network
import Data.Conduit.Text (encode, decode, utf8)
import qualified Data.Conduit.List as CL
import qualified Data.Conduit.Binary as CB
import Data.Text (toUpper)
import qualified Data.ByteString.Char8 as S8
@halcat0x15a
halcat0x15a / gist:3031170
Created July 2, 2012 04:54
Sudoku solver
(use 'clojure.core.logic)
(def _1 [()])
(def _2 [() ()])
(def _3 [() () ()])
(def _4 [() () () ()])
@tototoshi
tototoshi / trouble_with_jgit_and_JavaConversions.scala
Created July 16, 2012 02:42
trouble with jgit & scala.collection.JavaConversions
// import scala.collection.JavaConversions._
scala> val git = new Git(new FileRepositoryBuilder().findGitDir(new java.io.File("/path/to/repos")).readEnvironment().build())
git: org.eclipse.jgit.api.Git = org.eclipse.jgit.api.Git@18ed36fb
scala> git.log.addPath("foo").call.head
res49: org.eclipse.jgit.revwalk.RevCommit = commit c4554d9f46de49e449242a2aa75d39874dc99498 1342332998 ----sp
scala> git.log.addPath("foo").call.headOption
res50: Option[org.eclipse.jgit.revwalk.RevCommit] = Some(commit d3107ef998b25d70f86f1a5d530c31c02507d59e 1342012813 ----sp)
@lyricallogical
lyricallogical / parfun1.scala
Created August 31, 2012 08:49
難しいです
lazy val fab: PartialFunction[Base, Int] = {
case A(base) => 1 + fabc(base) // PartialFunction#apply の呼び出し。危ない!
case B(left, right) => 1 + fabcd(left) + fabcd(right) // 同様
}
// 全域関数だと scala コンパイラにはわからない!
lazy val fabcd: PartialFunction[Base, Int] = fab orElse {
case C => 1
case D => 2
}
@lyricallogical
lyricallogical / 1_bad.scala
Created September 29, 2012 12:27
ocaml like module(functor) programming in scala
imoprt Iteratee._
lazy val takeOdds = {
val aux = for {
one <- headOption[Byte[Array]] // ダサい
_ <- headOption[Byte[Array]] // ダサい
} yield one
for {
odds <- aux
@gakuzzzz
gakuzzzz / 1.scala
Created October 10, 2012 07:40
Scalaで文字列を1つずつずらす
scala> def add(i: Int)(words: String) = words.map({x: Char => x + i - 'a'} andThen Stream.continually(Range.inclusive('a', 'z')).flatten andThen {_.toChar})
add: (i: Int)(words: String)String
scala> add(2)("abcdz")
res16: String = cdefb
@etorreborre
etorreborre / gist:3870064
Created October 11, 2012 03:54
Unboxed union types with a context bound
/**
* this is an experiment to create unboxed union types with a phantom type and a context bound.
* All the good ideas come from @milessabin post, comments and links: http://www.chuusai.com/2011/06/09/scala-union-types-curry-howard/#comment-22
*/
/** trait for anything that can be A or B */
trait Or[A, B] {
// a phantom type, there will be no instance of this type that we'll use
type l[T]
// an alias for l[t]
@konn
konn / TuringMachine.hs
Created October 11, 2012 13:35
A simple turing machine simulator.
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module TuringMachine where
import Control.Applicative
import Control.Arrow hiding (left, right)
import Control.Lens hiding (Tape, Zipper, left, right)
import Data.List.Lens
import qualified Data.Map as M
import Data.Maybe
data Instruction s = GoLeft
@konn
konn / tex2a5.hs
Created October 14, 2012 07:20
自動的にトリミングしてA5に纏めなおすバッチ(要・shelly)
{-# LANGUAGE ExtendedDefaultRules, OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
module Main where
import Control.Monad
import qualified Data.List as List
import Data.Monoid
import qualified Data.Text.Lazy as LT
import Filesystem.Path.CurrentOS hiding (fromText, (<.>))
import Shelly
import System.Environment