Skip to content

Instantly share code, notes, and snippets.

View mariussoutier's full-sized avatar

Marius Soutier mariussoutier

View GitHub Profile
;; msoutier's solution to Replicate a Sequence
;; https://4clojure.com/problem/33
(fn [c n] (mapcat #(repeat n %) c))
;; msoutier's solution to Interpose a Seq
;; https://4clojure.com/problem/40
(fn [s c] (butlast (mapcat #(list %1 s) c)))
@mariussoutier
mariussoutier / gist:1376684
Created November 18, 2011 15:01
Game of Life in Scala
package keeponlyalive
/** A cell that exists is alive */
case class Cell(x: Int, y: Int) {
def survives(implicit world: Set[Cell]) = neighborCount(world) match {
case 2 => true
case 3 => true
case _ => false
}
@mariussoutier
mariussoutier / core.clj
Created November 18, 2011 08:54
Conway's Game of Life in Clojure
;;; Conway's Game of Life
;;; Using a set of coordinates to provide a infinite world
;;;
;;; Moritz Ulrich <ulrich.moritz@googlemail.com>
;;; Cologne Clojure User Group
;;; November 17, 2011
(ns game-of-life.core
(:use [clojure.set :as set]))
@mariussoutier
mariussoutier / gist:1669512
Created January 24, 2012 10:28
Edit user or display 404 Not Found in Play 2.0
UserDAO.findByEmail(email).map { user =>
Ok(views.html.edit(userForm.fill(user)))
}.getOrElse(NotFound("User not found!"))
@mariussoutier
mariussoutier / gist:3237595
Created August 2, 2012 14:49
repeat helper with index
package views.html.helper
import play.api.templates.Html
object repeatWithIndex {
def apply(field: play.api.data.Field, min: Int = 1)(f: (play.api.data.Field, Int) => Html) = {
(0 until math.max(if (field.indexes.isEmpty) 0 else field.indexes.max + 1, min)).map(i => f(field("[" + i + "]"), i))
}
}
@mariussoutier
mariussoutier / Helper.scala
Created August 7, 2012 14:28
Helper for using sequences in Play Scala templates
package views.html.helper
import play.api.templates.Html
object definingNonEmpty {
def apply[T <: Seq[_]](t: T)(block: (T) => Html) = {
if (t.nonEmpty) block(t) else Html("")
}
}
@mariussoutier
mariussoutier / Helper.scala
Created August 21, 2012 11:46
More template sequence helpers
package views.html.helper
import play.api.templates.Html
// Executes the first block if non-empty, the second otherwise
// Passes the entire seq (as in defining)
object ifEmptyOrElse {
def apply[T <: Seq[_]](t: T)(nonEmptyBlock: (T) => Html)(emptyBlock: => Html) = {
if (t.nonEmpty) nonEmptyBlock(t) else emptyBlock
}
# Map to language via RegEx
/$language<[a-z]{2}>/videos/:id controllers.Video.showVideo(id: Long, language: String)
# Hard-code supported languages
/de/videos/:id controllers.Video.showVideo(id: Long, language = "de")
/en/videos/:id controllers.Video.showVideo(id: Long, language = "en")
@mariussoutier
mariussoutier / 1Problem.scala
Last active December 16, 2015 05:49
Bridge Pattern in Scala
// Adding new functionality via inheritance leads to combinatorial explanation
abstract class Serializer {
def write(string: String): Unit
}
class CsvFileWriter extends FileWriter {
def writeFile(file: java.io.File) = //...
}