This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| : avg ( seq -- av ) [ sum ] [ length ] bi / ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" } ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |