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
| isPalindrome :: String -> Bool | |
| -- Pointful definition. | |
| isPalindrome s = s == reverse s | |
| -- Pointfree definition using applicative. | |
| isPalindrome = (==) <$> reverse <*> id | |
| -- Pointfree definition using monad. | |
| isPalindrome = reverse >>= (==) |
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
| import Control.Applicative | |
| import Control.Arrow | |
| import Control.Monad | |
| import Control.Monad.Instances | |
| import qualified Data.Vector as V | |
| -- Y U NO IN STANDARD LIBRARY????!!!! | |
| if' :: Bool -> a -> a -> a | |
| if' cond x y = if cond then x else y |
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
| // Required imports: scalaz._, Scalaz._, collection.immutable.HashMap | |
| scala> type Memory = HashMap[Symbol, Int] | |
| defined type alias Memory | |
| scala> def init(s: Symbol, v: Int) = state[Memory, Unit] { memory => | |
| | if(memory contains s) sys.error(s + " already in use!") | |
| | (memory + (s -> v), ()) | |
| | } | |
| init: (s: Symbol, v: Int)scalaz.State[Memory,Unit] |
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
| : par ( r1 r2 -- r ) [ + ] [ * ] 2bi / ; | |
| : pars ( r* -- r ) [ recip ] map-sum recip ; | |
| : safe-pars ( r* -- r ) 0 over member? [ "Zero value resistor" throw ] when pars ; |
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
| ! Problem #1 | |
| USING: combinators.short-circuit math.functions ; | |
| 1000 iota [ | |
| { [ 3 divisor? ] [ 5 divisor? ] } 1|| | |
| ] filter sum | |
| ! Problem #2 | |
| USE: fry | |
| : million ( n -- n' ) 1000000 * ; | |
| : fibonacci ( upto -- fib ) 0 1 rot '[ dup _ < ] [ swap over + dup ] produce 2nip but-last ; |
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
| trait Swappable[A] { | |
| def x: A | |
| def y: A | |
| def swap: MyType = { | |
| // implement this | |
| } | |
| } | |
| case class Foo[A](x: A, y: A) extends Swappable[A] | |
| case class Bar(x: Int, y: Int) extends Swappable[Int] |
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
| class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor { | |
| val timeToShutdown = 36000000 // msec | |
| val bidIncrement = 10 | |
| def act() { | |
| var maxBid = minBid - bidIncrement | |
| var maxBidder: Actor = null | |
| var running = true | |
| while (running) { | |
| receiveWithin ((closing.getTime() - new Date().getTime())) { | |
| case Offer(bid, client) => |
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
| import util.control.ControlThrowable | |
| case object RestartException extends ControlThrowable | |
| def restart = throw RestartException | |
| def attempt[A](f: => A) = new Attempt(f) | |
| class Attempt[A](f: => A) { | |
| def fallback(catcher: PartialFunction[Throwable, A]): 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
| class PeriodHelper(i: Int) { | |
| def seconds: Period = Period.seconds(i) | |
| } | |
| implicit def intToPeriodHelper(i: Int): PeriodHelper = new PeriodHelper(i) |
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
| case class Foo(i: Int) { | |
| def combine(that: Foo): Foo = Foo(this.i + that.i) | |
| def <+>(that: Foo): Foo = Foo(this.i + that.i) | |
| } | |
| val a, b = Foo(5) | |
| assert(a.combine(b) == Foo(10)) | |
| assert(a combine b == Foo(10)) | |
| assert(a.<+>(b) == Foo(10)) | |
| assert(a <+> b == Foo(10)) |