Skip to content

Instantly share code, notes, and snippets.

View missingfaktor's full-sized avatar
🥔

Rahul Goma Phulore missingfaktor

🥔
View GitHub Profile
isPalindrome :: String -> Bool
-- Pointful definition.
isPalindrome s = s == reverse s
-- Pointfree definition using applicative.
isPalindrome = (==) <$> reverse <*> id
-- Pointfree definition using monad.
isPalindrome = reverse >>= (==)
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
// 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]
: 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 ;
! 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 ;
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]
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) =>
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 = {
class PeriodHelper(i: Int) {
def seconds: Period = Period.seconds(i)
}
implicit def intToPeriodHelper(i: Int): PeriodHelper = new PeriodHelper(i)
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))