Skip to content

Instantly share code, notes, and snippets.

@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]
@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
@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
@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
}
@tk0miya
tk0miya / index.txt
Last active January 29, 2019 23:03
sphinxcontrib_wikitable.py
Usage
======
.. wiki-table::
:header-rows: 1
:widths: 2 3 5
|id|header1|header2|
|1|hello|world|
|2|foo|:strong:`bar`|
@xuwei-k
xuwei-k / Higher-Rank-Polymorphism-in-Scala.md
Created August 9, 2012 02:09
Higher-Rank Polymorphism in Scala

原文: http://apocalisp.wordpress.com/2010/07/02/higher-rank-polymorphism-in-scala/ 訳者は英語得意ではなく、訳が怪しいところがあるとおもうので、間違いやつっこみがあれば、ここのコメント欄に記入なりなんなり指摘お待ちしてます。

Scalaでの高ランクポリモーフィズム (Higher-Rank Polymorphism in Scala)

Tim Carstens のポスト Haskell features he’d like to see in other languages をよんで、そのなかの一つにrank-2 typesというものがありました。 Tim は、これ(rank-2 type)を使ったキラーアプリケーションとして、型システムを利用して、リソースへのアクセスの安全性を確保するといいます。

@swannodette
swannodette / gist:3217582
Created July 31, 2012 14:52
sudoku_compact.clj
;; based on core.logic 0.8-alpha2 or core.logic master branch
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
@matope
matope / Cassandra - A Decentralized Structured Storage System.markdown
Created July 23, 2012 09:56
Cassandra - A Decentralized Structured Storage System[和訳]
@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)
@halcat0x15a
halcat0x15a / gist:3031170
Created July 2, 2012 04:54
Sudoku solver
(use 'clojure.core.logic)
(def _1 [()])
(def _2 [() ()])
(def _3 [() () ()])
(def _4 [() () () ()])