Skip to content

Instantly share code, notes, and snippets.

@pjagielski
pjagielski / clojuredays_talk.clj
Last active August 24, 2022 02:37
My talk from Dutch Clojure Days 2017
(ns talk
(:require [overtone.core :refer :all]
[clojure.java.io :as io]))
;; Overtone by Sam Aaron & Jeff Rose
;; https://github.com/overtone/overtone
(definst da-funk [freq 440 dur 1.0 amp 1.0 cutoff 1700 boost 6 dist-level 0.015]
(let [env (env-gen (adsr 0.3 0.7 0.5 0.3) (line:kr 1.0 0.0 dur) :action FREE)
filter-env (+ (* freq 0.15)
@pjagielski
pjagielski / shape.rb
Last active September 12, 2021 16:18
Shape Of You in Sonic-Pi
use_bpm 100
ch1 = [73, 68, 61]
ch2 = [76, 68, 61]
ch3 = [73, 68, 61]
ch4 = [73, 69, 54]
ch5 = [76, 69, 54]
ch6 = [73, 69, 54]
ch7 = [73, 69, 57]
ch8 = [76, 69, 57]
(ns talk
(:require [overtone.core :refer :all]
[clojure.java.io :as io]
[clj-http.client :as http]
[clojure.test :as test]))
(def talk
{:title "Sequencing dance music with Clojure"
:author "Piotr Jagielski"
:company {:name "TouK" :what "Software house" :from "Warsaw, Poland"}
fun findAllBy(articleId: ArticleId): List<Comment>
// or
fun findAllByPaged(articleId: ArticleId, pageRequest: PageRequest): Page<Comment>
sealed class UserId : RefId<Long>() {
object New : UserId() {
override val value: Long by IdNotPersistedDelegate<Long>()
}
data class Persisted(override val value: Long) : UserId() {
override fun toString() = "UserId(value=$value)"
}
}
data class User(
@pjagielski
pjagielski / User.kt
Last active February 11, 2019 18:15
data class User(
val username: Username,
val password: String,
val email: String
)
object UserTable : Table("users") {
val username = text("username")
val email = text("email")
val password = text("password")
fun Iterable<ResultRow>.toArticles(): List<Article> {
return fold(mutableMapOf<ArticleId, Article>()) { map, resultRow ->
val article = resultRow.toArticle()
val tagId = resultRow.tryGet(ArticleTagTable.tagId)
val tag = tagId?.let { resultRow.toTag() }
val current = map.getOrDefault(article.id, article)
map[article.id] = current.copy(tags = current.tags + listOfNotNull(tag))
map
}.values.toList()
}
val ArticleWithTags = (ArticleTable leftJoin ArticleTagTable leftJoin TagTable)
override fun findBy(articleId: ArticleId) =
ArticleWithTags
.select { ArticleTable.id eq articleId }
.toArticles()
.singleOrNull()
override fun create(article: Article): Article {
val savedArticle = ArticleTable.insert { it.from(article) }
.getOrThrow(ArticleTable.id)
.let { article.copy(id = it) }
savedArticle.tags.forEach { tag ->
ArticleTagTable.insert {
it[ArticleTagTable.tagId] = tag.id
it[ArticleTagTable.articleId] = savedArticle.id
}
}
object ArticleTagTable : Table("article_tags") {
val tagId = tagId("tag_id").references(TagTable.id)
val articleId = articleId("article_id").references(ArticleTable.id)
}