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
| (defn evaluate [exp] | |
| (match exp | |
| (:variant :val [x]) x | |
| (:variant :add [x y]) (+ (evaluate x) (evaluate y)) | |
| (:variant :multiply [x y]) (* (evaluate x) (evaluate y)))) | |
| (def numeric-expression | |
| [:add [:val 2] | |
| [:multiply [:val 4] | |
| [:val 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
| (ns akar.talk-examples | |
| (:require [akar.try-out :refer :all] | |
| [clojure.xml :as xml]) | |
| (:import [java.util.concurrent ConcurrentHashMap] | |
| [java.io ByteArrayInputStream])) | |
| ; Example 1 | |
| (def request-ids-pattern | |
| #"rid=(.*)\|(.*)") |
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
| (require '[clojure.spec :as sp]) | |
| (defn define-syntax* [name' forms spec target] | |
| (let [enclosing-form (cons name' forms) | |
| result (sp/conform spec enclosing-form)] | |
| (if (= ::sp/invalid result) | |
| (throw (ex-info "Syntax error" (sp/explain-data spec enclosing-form))) | |
| (target result)))) | |
| (defmacro define-syntax [name & {:keys [spec-name spec target]}] |
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
| (defn find [seq' pred] | |
| (match seq' | |
| (:seq [(:and x [(!pred pred)]) & :_]) x | |
| (:seq [:_ & rest]) (find rest pred) | |
| (:seq []) nil)) |
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
| sealed trait JobDescription | |
| sealed trait HasN { | |
| def n: Int | |
| } | |
| case class JobOne(n: Int) extends JobDescription with HasN | |
| case object JobTwo extends JobDescription | |
| case class JobThree(n: Int) extends JobDescription with HasN |
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
| // 6,046 bytes jarred. | |
| /* | |
| Copyright 2012 Viktor Klang | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 |
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
| Prelude> newtype Dyn a = Dyn a deriving Show | |
| Prelude> data Book = Book (Dyn String) Int deriving Show | |
| Prelude> :{ | |
| Prelude| class Serialise a where | |
| Prelude| serialise :: a -> String | |
| Prelude| :} | |
| Prelude> instance Serialise Book where serialise (Book (Dyn s) i) = s ++ ":" ++ show i | |
| Prelude> | |
| Prelude> let serialisedBook = fmap serialise (\s -> Book s 32) | |
| Prelude> -- apply dynamic value when available |
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> case class Callback[A](f: (A => Unit) => Unit) { | |
| | def map[B](g: A => B): Callback[B] = Callback { (h: B => Unit) => f(a => h(g(a))) } | |
| | def flatMap[B](g: A => Callback[B]): Callback[B] = Callback { (h: B => Unit) => f(a => g(a).f(b => h(b))) } | |
| | } | |
| defined class Callback |
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
| package sudoku.console | |
| object Monadology { | |
| // Wear the function goggles. | |
| val foo = 2 + 3 | |
| val bar = foo + 1 | |
| Math.min(foo, bar) |
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
| package sudoku.errors | |
| import scala.language.higherKinds | |
| import scala.reflect.ClassTag | |
| object Modes { | |
| trait Mode { | |
| type Result[_, _] | |
| def run[E : ClassTag, A](a: => A): Result[E, A] |