Skip to content

Instantly share code, notes, and snippets.

@paulmillr
paulmillr / mapreduce.scala
Created March 10, 2012 16:03
Why functional programming matters (aka MapReduce for humans)
import com.cloudera.crunch._
import com.cloudera.scrunch._
class ScrunchWordCount {
def wordCount(inputFile: String, outputFile: String) = {
val pipeline = new Pipeline[ScrunchWordCount]
pipeline.read(from.textFile(inputFile))
.flatMap(_.toLowerCase.split("\\W+"))
.filter(!_.isEmpty())
.count
#include <memory>
#include <iostream>
#include <sstream>
#include <utility>
#include <algorithm>
#include <iterator>
struct sequence_tag {};
struct pointer_tag {};
@danneu
danneu / golang-vs-clojure-async.md
Last active November 6, 2023 04:09
Google I/O 2012 - Go Concurrency Patterns ported to Clojure Video: http://www.youtube.com/watch?v=f6kdp27TYZs
@ghoseb
ghoseb / prime_sieve.clj
Last active April 22, 2016 08:05
A concurrent prime sieve in Clojure using core.async
(ns prime-sieve
(:require [clojure.core.async :as async :refer [chan go <! >!]]))
;;; concurrent prime sieve in Clojure using core.async
;; inspired by a similar implementation in Go
;; http://golang.org/doc/play/sieve.go
(defmacro go-forever
"An infinite loop that runs in a go block."
@jamarparris
jamarparris / Generate Mongo Object ID in PostGres
Last active March 20, 2024 00:04
Create ObjectIds in PostGres following the MongoDB semantics. Very similar to the Instagram approach linked below.http://docs.mongodb.org/manual/reference/object-id/http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram
The MIT License (MIT)
Copyright (c) 2013 Jamar Parris
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE S
@micmarsh
micmarsh / flip.clj
Last active May 12, 2022 17:16
Flip the arguments of a function, Clojure style
(defn flip [function]
(fn
([] (function))
([x] (function x))
([x y] (function y x))
([x y z] (function z y x))
([a b c d] (function d c b a))
([a b c d & rest]
(->> rest
(concat [a b c d])
@vjache
vjache / SRB
Last active August 29, 2015 14:07
Deadly simple Shared Ring Buffer in Erlang.
%% Shared Ring Buffer
-module(srb).
-export([new/2, push/2, pop/2, pop/1]).
-compile(export_all).
-record(counters,
{ clock :: non_neg_integer(),
max_size :: non_neg_integer() }).
@azymnis
azymnis / KMeansJob.scala
Created October 23, 2014 23:07
K-Means in scalding
import com.twitter.algebird.{Aggregator, Semigroup}
import com.twitter.scalding._
import scala.util.Random
/**
* This job is a tutorial of sorts for scalding's Execution[T] abstraction.
* It is a simple implementation of Lloyd's algorithm for k-means on 2D data.
*
* http://en.wikipedia.org/wiki/K-means_clustering
@invkrh
invkrh / mockInput.scala
Last active November 24, 2017 10:15
input mocking for test need
object test extends App {
import scala.collection.JavaConversions.asJavaEnumeration
import java.io.{ByteArrayInputStream, SequenceInputStream}
val inputs = Iterator("asdf", "qewr", "wert")
.map(x => new ByteArrayInputStream((x + "\n").getBytes))
val in = new SequenceInputStream(asJavaEnumeration(inputs))
Console.setIn(in)
val a = readLine()
val b = readLine()
@non
non / operators-for-cats.md
Last active January 5, 2018 15:26
Basic write-up of how Cats supports using symbolic operators with type classes.

Symbolic operators and type classes for Cats

One of the simplest and most recognizable type classes is the semigroup. This type class abstracts over the ability to combine values of a certain type in an associative manner.

Cats provides cats.Semigroup[A] to model semigroups [1]. The combine method takes two values of the type A and returns an A value.