Skip to content

Instantly share code, notes, and snippets.

View missingfaktor's full-sized avatar
🥔

Rahul Goma Phulore missingfaktor

🥔
View GitHub Profile
scala> trait DayOfWeek; trait Sun extends DayOfWeek; trait Mon extends DayOfWeek // etc
defined trait DayOfWeek
defined trait Sun
defined trait Mon
scala> trait Weekend[D <: DayOfWeek]; implicit object SunInWeekend extends Weekend[Sun] // define one for Sat too
defined trait Weekend
defined module SunInWeekend
scala> def goSkiing[D <: DayOfWeek : Weekend] = println("whee")
scala> type of[T[_], A] = T[A]
defined type alias of
scala> val xs: List of Int = List(3, 4, 5)
xs: of[List,Int] = List(3, 4, 5)
scala> def not: Boolean => Boolean = ! _
not: Boolean => Boolean
scala> def odd: Int => Boolean = _ % 2 != 0
odd: Int => Boolean
scala> List(3, 11, 4).filter(not compose odd)
res93: List[Int] = List(4)
// As Daniel said here[http://twitter.com/#!/djspiewak/status/129588554723639296], Scala's traits can be used as
// polymorphic modules. I was saying the same thing the other day.
// objects (both regular and the ones declared with 'object' keyword) serve as modules. If you want parametrized
// modules, trait + object combination should be used. A made up example below:
// This is our parametrized module.
trait PhysicsEngine {
// parameters
type P <: Planet
def coeffOfGravity: Double
: avg ( seq -- av ) [ sum ] [ length ] bi / ;
USING: math.ranges io.encodings.utf8 xml concurrency.combinators ;
! 1. Multiply Each Item in a List by 2
10 [1,b] [ 2 * ] map
! 2. Sum a List of Numbers
1000 [1,b] sum
! 3. Verify if Exists in a String
: word-list ( -- seq ) { "Factor" "Concatenative" "Point-free" } ;
@missingfaktor
missingfaktor / cond.scala
Created November 14, 2011 11:47
"cond" for Scala
def cond[A, B](value: A)(cases: (A => Boolean, B)*): B = {
cases.collectFirst({ case (c, v) if c(value) => v }).getOrElse(sys.error("Match error!"))
}
def otherwise[A] = Function.const(true)(_: A)
scala> trait Lol
defined trait Lol
scala> case class Wut(x: Int)
defined class Wut
scala> val y = Wut(13)
y: Wut = Wut(13)
scala> val z = y match {
@missingfaktor
missingfaktor / missingfaktor.xml
Created December 10, 2011 07:24
IntelliJ IDEA color scheme
<?xml version="1.0" encoding="UTF-8"?>
<scheme name="missingfaktor" version="1" parent_scheme="Default">
<option name="LINE_SPACING" value="1.0" />
<option name="EDITOR_FONT_SIZE" value="13" />
<option name="EDITOR_FONT_NAME" value="Monaco" />
<colors />
<attributes>
<option name="ABSTRACT_CLASS_NAME_ATTRIBUTES">
<value>
<option name="FOREGROUND" value="cc0000" />
haskell>let xs # ys = concat $ zipWith (\x y -> take x $ repeat y) xs ys
haskell>[1..9] # [1..9]
[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9]
haskell>let (#>) = join (#)
haskell>(#>) [1..9]
[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9]