Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
boraseoksoon / merge-maps.cljs
Last active September 11, 2022 22:47
merge maps into a unified one map.
(defn merge-maps [& maps]
;; (sanitize-map)
(if (every? map? maps)
(apply merge-with merge-maps maps)
(last maps)))
(defn sanitize-map [m]
(if (map? m)
(let [clean-val (fn [[k v]]
(let [v' (sanitize-map v)]
@boraseoksoon
boraseoksoon / .swift
Last active June 16, 2022 04:44
a reversed value semantic enum list using indirect enum
import Foundation
// today coding: write a reversed value semantic enum list.
indirect enum List<T> {
case empty
case node(T, List<T>)
}
extension List {
@boraseoksoon
boraseoksoon / .swift
Created June 3, 2022 23:02
if case let
indirect enum List<T> {
case empty
case node(T, List<T>)
var value: T? {
if case .node(let value, _) = self {
return value
} else {
return nil
}
@boraseoksoon
boraseoksoon / enumList.swift
Last active August 13, 2022 20:01
a value semantic enum list that can map filter reduce using indirect enum
import Foundation
// today coding: implement map filter reduce of the value semantic enum list.
indirect enum List<T> {
case empty
case node(T, List<T>)
}
extension List {
@boraseoksoon
boraseoksoon / .swift
Created May 29, 2022 04:19
A basic Async Sequence in Swift for HTML Parsing
import SwiftUI
// today coding: async sequence in Swift
struct URLSequence: AsyncSequence {
typealias AsyncIterator = URLIterator
typealias Element = Data
let urls: [URL]
@boraseoksoon
boraseoksoon / fib.swift
Last active July 2, 2022 13:35
7 ways to kill Fibonacci sequence in Swift.
import Foundation
// daily coding 2022/05/06
// seven ways to solve fibonnaci in Swift.
// (0) imperative style
// (1) using any sequence and iterator
// (2) normal recursion version
// (3) shorter recursion version
@boraseoksoon
boraseoksoon / .swift
Created April 30, 2022 22:42
[algorithm] new way to implement Fibonacci using closure for any iterator and any sequence.
let res4 = Array(
AnySequence { () -> AnyIterator<Int> in
var t1 = (0, 1)
return AnyIterator<Int> {
let (a, b) = t1
t1 = (b, a + b)
return b
}
}
.prefix(10)
@boraseoksoon
boraseoksoon / .swift
Created April 20, 2022 04:06
a growing closure
import Foundation
struct Recursion {
/// Generate `a growing closure` to keep track of the all of history of task, and
/// run a function as a given parameter after executing all of previous tasks if tasks exist, and add the function to the tasks.
/// - Returns: `the growing closure` to remember the tasks to execute.
///
/// ```
/// let recur = Recursion.generateGrowingClosure()
@boraseoksoon
boraseoksoon / .swift
Created April 20, 2022 03:02
growing a recursion
import Foundation
struct Recursion {
static func generated()
-> ((() -> Void)?) -> ()
{
var tasks: [(() -> ())?] = []
let closure = { (task: (() -> Void)?) in
tasks.append(task)
@boraseoksoon
boraseoksoon / .swift
Last active April 19, 2022 21:55
a growing recursion
import Foundation
struct Recursion {
static func recur(task baseTask: @escaping () -> Void,
willGrow: Bool = true)
-> (( () -> Void)?) -> Void
{
var next = 0
return { lastTask in