Skip to content

Instantly share code, notes, and snippets.

View mariussoutier's full-sized avatar

Marius Soutier mariussoutier

View GitHub Profile
// Scala - you gotta love it.
class MPSList[T](val list: List[T]) {
def removeByIndex(index: Int): MPSList[T] = {
new MPSList[T](list.remove(list.indexOf(_) == index))
}
}
object MPSList {
implicit def mpsListToScalaList[T](list: List[T]) = new MPSList[T](list)
; It's a thing of beauty how easy it is to parse therapy intervals in Clojure.
; Therapy intervals are usually Strings like 1-0-3-0 or 1-0.5-1 to describe how many pills a patient is taking every day
(defn interval-value [interval] "Parses an interval String and returns its numerical value"
(reduce + (map #(Double/valueOf %) (re-seq #"[0-9]+(?:[\\.][0-9]+)?" interval))))
// "Write less, do more" episode 1203: iterating Strings creates chars, yielding chars creates a String
def abbreviatedName(fullName: String) = for (ch <- fullName; if ch.isUpper) yield ch
// Best of both worlds - WebObjects + Scala
// valueForKeyPath operators
def parameters: NSArray[Parameter] = ...
def maxSortOrder = parameters.valueForKeyPath("sortOrder.@max").asInstanceOf[Int]
// Clone attributes of an EnterpriseObject (aka DB row with all non-relationship columns)
trait Cloning extends EOEnterpriseObject {
type EOType <: EOEnterpriseObject
;; marius' solution to Compress a Sequence
;; https://4clojure.com/problem/30
#(map last (partition-by str %))
;; msoutier's solution to Drop Every Nth Item
;; https://4clojure.com/problem/41
(fn [coll n]
(keep-indexed #(if (not= 0 (mod (inc %1) n)) %2 nil) coll))
;; msoutier's solution to Duplicate a Sequence
;; https://4clojure.com/problem/32
#(interleave % %)
;; msoutier's solution to Pack a Sequence
;; https://4clojure.com/problem/31
partition-by identity
;; 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)))