Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
@folone
folone / gist:6089236
Last active October 10, 2020 18:26
Table of unicode operators in scalaz 6.0.x
@folone
folone / SCombinator.scala
Created June 30, 2011 07:19
Y-Combinator in Scala
/**
* <b>Fixed Point Combinator is:</b>
* Y = λf.(λx.f (x x)) (λx.f (x x))
*
* <b>Proof of correctness:</b>
* Y g = (λf . (λx . f (x x)) (λx . f (x x))) g (by definition of Y)
* = (λx . g (x x)) (λx . g (x x)) (β-reduction of λf: applied main function to g)
* = (λy . g (y y)) (λx . g (x x)) (α-conversion: renamed bound variable)
* = g ((λx . g (x x)) (λx . g (x x))) (β-reduction of λy: applied left function to right function)
* = g (Y g) (by second equality) [1]
@folone
folone / hello-ski.hs
Created August 27, 2012 08:16 — forked from shangaslammi/hello-ski.hs
Hello World using Applicative instance for ((->) r) as a computational basis
$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l hello-ski.hs
[1 of 1] Compiling Main ( hello-ski.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello world!
@folone
folone / gist:6258410
Last active April 27, 2018 14:09
Ackermann function
def ackermann(m: Int, n: Int): Int = {
(m, n) match {
case (0, _) ⇒ n + 1
case (m, 0) if m > 0 ⇒ ackermann(m - 1, 1)
case (m, n) if m > 0 && n > 0 ⇒ ackermann(m - 1, ackermann(m, n - 1))
}
}
import scalaz._, Scalaz._, Free.{suspend ⇒ _, _}, Trampoline._
def ackermannTramp(m: Int, n: Int): Trampoline[Int] = {
@folone
folone / kind.scala
Last active February 20, 2018 22:18 — forked from eed3si9n/kind.scala
// Updated for Scala 2.10.0.
def kind[A: scala.reflect.runtime.universe.TypeTag](a: A): String = {
import scala.reflect.runtime.universe._
def typeKind(sig: Type): String = sig match {
case PolyType(params, resultType) =>
(params map { p =>
typeKind(p.typeSignature) match {
case "*" => "*"
case s => "(" + s + ")"
}
### Keybase proof
I hereby claim:
* I am folone on github.
* I am folone (https://keybase.io/folone) on keybase.
* I have a public key whose fingerprint is 3A57 D1D4 67F6 0189 3CB7 03D8 FA5C 9955 37B3 71EF
To claim this, I am signing this object:
import scalaz._, Scalaz._
case class Happy[A](a: A)
case class NewYear(year: Int)
implicit val happyInstance = new Applicative[Happy] {
def point[A](a: ⇒ A) = Happy(a)
def ap[A, B](fa: ⇒ Happy[A])(f: ⇒ Happy[A => B]) =
f match {
case Happy(f) ⇒ fa match {
@folone
folone / gist:4945168
Last active April 27, 2017 13:10
A taste of dependent types in scala with shapeless.
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
@folone
folone / so.scala
Last active December 19, 2015 23:59
> shapeless-examples/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[warn] Binary version (2.10) for dependency org.scala-lang#scala-library;2.10.2
[warn] in com.chuusai#shapeless_2.10.2;2.0.0-SNAPSHOT differs from Scala binary version in project (2.10.2).
[warn] Binary version (2.10) for dependency org.scala-lang#scala-compiler;2.10.2
[warn] in com.chuusai#shapeless_2.10.2;2.0.0-SNAPSHOT differs from Scala binary version in project (2.10.2).
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_40).
Type in expressions to have them evaluated.
@folone
folone / gist:5766269
Last active December 18, 2015 10:09
Parallelizing lazy Streams.
final val BATCH_SIZE = (Runtime.getRuntime.freeMemory / 1024).toInt // kb per object should be enough, right?
def doStuff(m: MyType): Stream[MyType] = ??? // Costly operation
val s: Stream[MyType] = ???
s.grouped(BATCH_SIZE).toStream.flatMap(_.par flatMap doStuff)