Skip to content

Instantly share code, notes, and snippets.

@ayosec
ayosec / README.md
Last active August 29, 2015 13:56
Sweet.js: Better for

Add for(value <- items) and for(value, offset <- items) to Sweet.js

Examples

for(x <- [1,2,3,4]) {
  var a = x + 10;
  print(a);
}
(import '[javax.script ScriptEngineManager])
(let [engine (.getEngineByName (ScriptEngineManager.) "nashorn")]
(println (= (.eval engine "' \\r\\n' == 0") true)))
@aaronpowell
aaronpowell / null-guard.sjs
Last active August 29, 2015 13:58
Trying out the C# vNext `?.` operator in Sweet.js
macro null_helper {
rule { ($processed ...) ($rest) } => {
$processed (.) ... . $rest
}
rule { ($processed ...) ($rest_head $rest ...) } => {
$processed (.) ... . $rest_head
&& null_helper ($processed ... $rest_head) ($rest ...)
}
}
(defn flatmap<
"Takes a function and a source channel, and returns a channel which
contains the values in each channel produced by applying f to
each value taken from the source channel. f must return a
channel."
[f in]
(let [out (a/chan)]
(a/go-loop []
(let [item (a/<! in)]
(if (nil? item)
@jhaberstro
jhaberstro / gist:878f7f2043f922f0d06c
Last active August 29, 2015 14:02
Multiple optional bindings in Swift (i.e. the equivalent of if let (a, b, c) = (optA, optB, optC) { ... })
// The DSL's implementation (i.e. just the applicative interface for Optionals + methods for creating tuples)
operator infix <*> { associativity left }
@infix func <*><A, B>(lhs: (A -> B)?, rhs: A?) -> B? {
switch lhs {
case .None:
return .None
case let .Some(f):
return rhs.map(f)
}
}
macro protocol {
case { _ $name:ident { $fn:protocol_fn ... } } => {
// Probably not a good idea to rely on `__fresh`. Sweet.js should provide
// some sort of blessed gensym capability.
letstx $id = [makeValue(__fresh(), #{ here })];
return #{
function $name(proto, impl) {
if (!impl) {
impl = proto;
proto = {};
@frenchy64
frenchy64 / schema.md
Created August 11, 2014 06:55
Schema vs core.typed

Schema

I think of Schema as a runtime contracts library (I believe it does coercion and validation, but they seem related to me).

Pros

  • small barrier to entry, thus immediately useful
    • just add a contract/coercion to a function and you're off
  • can manipulate contracts as regular clojure data
  • documentation
@ceedubs
ceedubs / gist:a96bc169eb3152cd6226
Created September 30, 2014 22:17
Scalaz's Equal type class (and associated handy === syntax) is a type-safe alternative to Scala's ==
scala> case class UserId(id: Long)
defined class UserId
scala> def isSusan(id: UserId): Boolean = id == 3L // see the bug?
isSusan: (id: UserId)Boolean
scala> isSusan(UserId(3L))
res31: Boolean = false// shouldn't this be true?
// let's show the compiler how to check whether two UserIds are equal
@martintrojer
martintrojer / kebab.clj
Last active August 29, 2015 14:07
kebab-case keywords in nested data-structures
(ns kebab
(:require [camel-snake-kebab :as kebab]
[schema.coerce :as c]
[schema.core :as s]))
(def Data (s/either s/Keyword
{(s/recursive #'Data) (s/recursive #'Data)}
[(s/recursive #'Data)]
#{(s/recursive #'Data)}
s/Any))
@diversit
diversit / mockito-optional-empty-returns.java
Last active August 29, 2015 14:10
Mockito: return default Optional.empty for unstubbed methods
public static final Answer DEFAULT_OPTIONAL_EMPTY_ANSWER = invocation ->
invocation.getMethod().getReturnType() == Optional.class ? Optional.empty() : null;
Mockito.mock(YourClassWithOptionalReturnValues.class, DEFAULT_OPTIONAL_EMPTY_ANSWER);
// unfortunately this does not work since Mockito's Answers enum is not extendable (yet, issue 345)
@Mock(answer = DEFAULT_OPTIONAL_EMPTY_ANSWER)
private YourClassWithOptionalReturnValues mockInstance;