Skip to content

Instantly share code, notes, and snippets.

let suffix n xs = List.toSeq xs |> Seq.skip (xs.Length - n) |> Seq.toList
let removeLast n xs = List.toSeq xs |> Seq.rev |> Seq.removeManyAt 0 n |> Seq.rev |> Seq.toList
let span fn xs = (List.takeWhile fn xs, List.skipWhile fn xs)
@fwgreen
fwgreen / Toolz.hs
Last active June 15, 2023 05:15
Chunk and friends
chunq :: Int -> [a] -> [[a]]
chunq _ [] = []
chunq n xs = let (first, remaining) = splitAt n xs in first : chunq n remaining
transpoze :: [[a]] -> [[a]]
transpoze [] = []
transpoze ([] : xss) = transpoze xss
transpoze ((x:xs) : xss) = (x : [h | (h:_) <- xss]) : transpoze (xs : [ t | (_:t) <- xss])
@fwgreen
fwgreen / MakeString.swift
Created April 2, 2023 04:40
A simple Swift version of Scala's mkString
extension Collection {
func makeString(_ separator: String) -> String { makeString("", separator, "") }
func makeString(_ prefix: String, _ separator: String, _ suffix: String) -> String {
if isEmpty {
return prefix + suffix
} else {
return addString("", prefix, separator, suffix)
}
}
@fwgreen
fwgreen / patient_room_view.sql
Created March 28, 2023 21:50
ORMish view for row mapper
DROP VIEW IF EXISTS patient_room_view;
CREATE VIEW patient_room_view AS
SELECT
nt.room_number,
CASE
WHEN coalesce(jsonb_agg(tm.*) FILTER (WHERE tm.job_title = 'RN'), '{}') ->> 0 != '[null]'
THEN (coalesce(jsonb_agg(tm.*) FILTER (WHERE tm.job_title = 'RN'), '{}') ->> 0)::jsonb
ELSE '{}'
END
AS nurse,
@fwgreen
fwgreen / paginated.sql
Created March 23, 2023 21:26
Paginating SQL function
CREATE OR REPLACE FUNCTION list_paginated_messages_by_priority(p text, o int, l int)
RETURNS TABLE(id bigint, sender_name text, recipient_name text, message_title text, current_page bigint, page_count bigint)
LANGUAGE SQL
BEGIN ATOMIC
WITH pagination_meta_data AS (
SELECT *,
((ROW_NUMBER () OVER (ORDER BY id) - 1) / $3) + 1 AS current_page,
CEILING((COUNT(*) OVER ())::decimal / $3::decimal)::bigint AS page_count
FROM (
SELECT *,
@fwgreen
fwgreen / adt_enum.java
Last active October 4, 2021 20:42
More powerful Enums based on ADTs
enum Suit(String symbol) {
Hearts("♡"), Clubs("♣"), Spades("♠"), Diamonds("♢");
}
//the above generates...
sealed interface Suit permits Hearts, Clubs, Spades, Diamonds {
String symbol();
}
record Hearts() implements Suit {
@Override
@fwgreen
fwgreen / index.html
Created February 16, 2019 03:44
Basic html5 page with stylesheet
<!DOCTYPE html>
<html>
<head>
<title>Web Page</title>
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width">
</head>
<body>
<main>
<header>header</header>
@fwgreen
fwgreen / CollectionUtils.java
Last active April 2, 2020 13:47
Collection utilities I tend to forget how to implement
<E> List<List<E>> chunk(List<E> list, int size) {
int length = list.size();
return IntStream.range(0, (length - 1) / size + 1)
.mapToObj(i -> list.subList(i *= size, length - size >= i ? i + size : length))
.collect(Collectors.toList());
}
<T, U> Map<T, U> zip(List<T> first, List<U> second) {
return IntStream.range(0, Math.min(first.size(), second.size())).boxed()
.collect(Collectors.toMap(first::get, second::get));
@fwgreen
fwgreen / AsyncRegex.kt
Created January 29, 2019 05:52
RegexRedux in Kotlin with coroutines
import kotlinx.coroutines.*
import java.io.File
fun main() = runBlocking {
val start = System.nanoTime()
val input = File("file_path.txt").readText(Charsets.UTF_8)
val sequence = ">.*\n|\n".toRegex().replace(input, "")
val replacements = listOf(
@fwgreen
fwgreen / Extensions.swift
Last active September 24, 2020 06:59
Simple utility extensions for Swift
///Scan function that starts with the first element of the Sequence, like fold
extension Sequence {
func scan(_ combiner: (Element, Element) throws -> Element) rethrows -> [Element] {
var iterator = makeIterator()
guard let initial = iterator.next() else {
return []
}
var firstIteration = true
var accumulator = initial
return try map { element in