Skip to content

Instantly share code, notes, and snippets.

List(("A", 1), ("B", 2)).map(second)
def second[A, B](a: (A, B)) = (a._2, a._1)
def second2[A, B](a: (A, B)) = a match {
case (x, y) => (y, x)
}
def second3[A, B]: (A, B) => (B, A) = { case(a, b) => (b, a) }
def second4[A, B]: (A, B) => (B, A) = { case(a, b) => b -> a }
SELECT
(data->'backofficeInformation'->'customerNumber')::int,
identities->'countryCode'
FROM customers
cross join lateral jsonb_array_elements(data->'personalInformation'->'identities') identities
WHERE (data->'backofficeInformation'->'customerNumber')::int = 900198808
and (identities->>'isTaxCountry')::boolean = true
--same as
val combinationsLetters = (for {
(i, idi) <- List("A", "B", "C", "D", "E").zipWithIndex
(j, idj) <- List("A", "B", "C", "D", "E").zipWithIndex.take(idi)
(k, _) <- List("A", "B", "C", "D", "E").zipWithIndex.take(idj)
} yield (i, j, k))
val combinationsNumbers = (for {
(i, idi) <- List("1", "2", "3").zipWithIndex
(j, _) <- List("1", "2", "3").zipWithIndex.take(idi)
} yield (i, j))
(ns griffin.spec.journal-entry
(:require [clojure.spec.alpha :as s]))
(s/def ::id uuid?)
(s/def ::account-id uuid?)
(s/def ::type #{:credit :debit})
(s/def ::amount pos-int?)
(s/def ::line-item (s/keys :req-un [::account-id ::amount ::type]))
(s/def ::line-items (s/coll-of ::line-item :min-count 2))
(s/def ::journal-entry (s/keys :req-un [::id ::line-items]))
object MissingNumber extends App {
def missingNumber(nums: Array[Int]): Int = {
val found = new Array[Int](nums.length)
for (n <- nums) {
if (n < nums.length) found(n) = 1
}
val missing = found.indexOf(0)
@renanreismartins
renanreismartins / MissingPositive.scala
Created February 21, 2023 15:23
Procedural approach
object MissingPositive extends App {
def missingPositive(nums: Array[Int]): Int = {
//val m1: Map[Int, Int] = (0 until nums.length).foldLeft(Map[Int, Int]())((m, i) => if (nums(i) > 0) m + (i -> 1) else m)
val found = scala.collection.mutable.Map[Int, Int]()
var min = Integer.MAX_VALUE
@renanreismartins
renanreismartins / MissingPositive2.scala
Created February 21, 2023 15:48
A bit better, much to be done. Maybe do both operations in one traverse, fix the imperative checks and 0 checks. But enough for the day
object MissingPositive extends App {
def missingPositive(nums: Array[Int]): Int = {
val found: Map[Int, Int] = nums.foldLeft(Map[Int, Int]())((m, i) => if (i >= 0) m + (i -> 1) else m)
val min = nums
.filter(e => e > 0)
.foldLeft(Integer.MAX_VALUE)((m, i) => i.min(m))
if (min >= 0) {
var count = 1
@renanreismartins
renanreismartins / canSum.scala
Created March 1, 2023 22:27
coin change problem
object CanSum extends App {
def canSum(n: Int, coins: Array[Int]): Boolean = {
if (n == 0) return true
for (coin <- coins) {
val rest = n - coin
if (rest >= 0) return canSum(rest, coins)
}
@renanreismartins
renanreismartins / CanSum.scala
Created March 1, 2023 23:05
move the stop to the beginning
object CanSum extends App {
def canSum(n: Int, coins: Array[Int]): Boolean = {
if (n == 0) return true
if (n < 0) return false
for (coin <- coins) {
val rest = n - coin
return canSum(rest, coins)
@renanreismartins
renanreismartins / HowSum.scala
Last active March 2, 2023 10:29
null pointers because of the null return when there is no combination, but fine
object HowSum extends App {
def howSum(n: Int, coins: Array[Int]): Array[Int] = {
if (n == 0) return Array[Int]()
if (n < 0) return null
for (coin <- coins) {
val rest = n - coin
val ints = howSum(rest, coins)