Skip to content

Instantly share code, notes, and snippets.

/**
* @return example: '0zz12948ab0', length fixed
*/
function getRandomString(): string {
const rand = Math.random() * 2 ** 52;
const pad = '00000000000';
const padded = pad + rand.toString(36);
return padded.substr(padded.length - pad.length);
}
@jeiea
jeiea / useInterval.jsx
Created June 26, 2019 02:23
React hook version of useInterval
import { useEffect, useState } from 'react';
function useInterval(interval, setter) {
const [updater] = useState({});
updater.update = setter;
useEffect(() => {
const id = setInterval(() => {
updater.update(c => c + 1);
}, interval);
return () => clearInterval(id);
@jeiea
jeiea / HtmlToPlainText.kt
Created February 12, 2019 09:34
html to plain text kotlin port of https://stackoverflow.com/a/50363077
/**
* It depends on JSoup.
* Kotlin port of https://stackoverflow.com/a/50363077
*/
object Utils {
private val block = "address|article|aside|blockquote|canvas|dd|div|dl|dt|" +
"fieldset|figcaption|figure|footer|form|h\\d|header|hr|li|main|nav|" +
"noscript|ol|output|p|pre|section|table|tfoot|ul|video"
private val rxlongWSpaces = Regex("""\s{2,}""")
private val rxNestedBlock = Regex("""(\s*?</?(${block})[^>]*?>)+\s*""", RegexOption.IGNORE_CASE)
@jeiea
jeiea / diff-interface-typeclass.md
Last active February 1, 2019 10:36
Difference between OOP interfaces and FP type classes ๋ฒˆ์—ญ

์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ด€์ ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ณต๊ฐ๋ฐ›์ง„ ๋ชปํ•˜๋”๋ผ๋„, OOP์˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์ข‹์€ ์ถœ๋ฐœ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค (๋‹น์—ฐํžˆ ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฅด๋Š” ๊ฒƒ๋ณด๋‹จ ๋‚˜์„ ๊ฒƒ์ด๋‹ค). ์‚ฌ๋žŒ๋“ค์€ ๊ฐœ๋…์ ์œผ๋กœ ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ํƒ€์ž…์„ ์ง‘ํ•ฉ์ฒ˜๋Ÿผ ๊ตฌ๋ถ„ํ•œ๋‹ค๊ณ  ๋งํ•œ๋‹ค. "ํŠน์ • ์—ฐ์‚ฐ๊ณผ, ์–ธ์–ด ์ž์ฒด๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๊ธฐ๋Œ€๋“ค์„ ๋งŒ์กฑํ•˜๋Š” ํƒ€์ž…๋“ค์˜ ์ง‘ํ•ฉ"์ฒ˜๋Ÿผ ๋ง์ด๋‹ค. ์ด ๋ง์€ ํƒ€๋‹นํ•˜๊ณ , "์ด ํƒ€์ž…์ด ์š”๊ตฌ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ์ด ํƒ€์ž…์„ ์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค๋ผ"๋Š” ์•„๋ฌด ๋ฉ”์†Œ๋“œ ์—†๋Š” ํƒ€์ž… ํด๋ž˜์Šค๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฑด ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” ๊ฑฐ์˜ ๋ณผ ์ˆ˜ ์—†๋Š” ํ™œ์šฉ์ด๋‹ค.

๊ตฌ์ฒด์ ์ธ ์ฐจ์ด์ ์„ ๋งํ•˜์ž๋ฉด, OOP ์ธํ„ฐํŽ˜์ด์Šค๋ณด๋‹ค ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ๋” ๊ฐ•๋ ฅํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค:

@jeiea
jeiea / 10.3-layout.md
Created December 27, 2018 04:45
ํ•˜์Šค์ผˆ 2010 ๋ ˆํฌํŠธ ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™์„ ํ•„์š”์— ์˜ํ•ด ๋ฒˆ์—ญํ•จ

10.3 ๋ ˆ์ด์•„์›ƒ(์ดํ•˜ ๋“ค์—ฌ์“ฐ๊ธฐ)

2.7์žฅ์—์„œ ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ ๋” ์ž์„ธํžˆ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์Šค์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋Š” ๋“ค์—ฌ์“ฐ๊ธฐ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ค‘๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์ ์ ˆํžˆ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋“ค์—ฌ์“ฐ๊ธฐ์˜ ํšจ๊ณผ๋ฅผ ์™„์ „ํžˆ ๊ฐˆ์Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•œ ํ”„๋กœ๊ทธ๋žจ์€ ๋“ค์—ฌ์“ฐ๊ธฐํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง‘๋‹ˆ๋‹ค.

๋“ค์—ฌ์“ฐ๊ธฐ๋œ ํ”„๋กœ๊ทธ๋žจ์— ์–ด๋–ป๊ฒŒ ์ค‘๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์ถ”๊ฐ€ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•ด์„œ ๋“ค์—ฌ์“ฐ๊ธฐ์˜ ํšจ๊ณผ๋ฅผ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ ๋ณ€ํ™˜ ํ•จ์ˆ˜ L์„ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. L์˜ ์ž…๋ ฅ์€:

  • ์•„๋ž˜ ํ† ํฐ์ด ์ถ”๊ฐ€๋œ ํ•˜์Šค์ผˆ ๋ ˆํฌํŠธ ์–ดํœ˜ ๊ตฌ๋ฌธ์— ์ •์˜๋œ ์–ดํœ˜์†Œlexeme ์ŠคํŠธ๋ฆผ:
  • let, where, do, of ํ‚ค์›Œ๋“œ ๋’ค์— {๊ฐ€ ์—†์œผ๋ฉด {n} ํ† ํฐ์ด ๊ทธ ํ‚ค์›Œ๋“œ ๋’ค์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. n์€ ๋‹ค์Œ ์–ดํœ˜์†Œ์˜ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์„ ์˜๋ฏธํ•˜๊ณ  ํŒŒ์ผ์˜ ๋์ผ ๋•Œ๋Š” 0์ด ๋ฉ๋‹ˆ๋‹ค.
@jeiea
jeiea / TsvReader.kt
Last active November 3, 2018 20:04
For those who file.readText().lines().map { it.split('\t') } is not sufficient
import java.io.Reader
/**
* Portable tsv reader supporting multiline
* Usage: File("a.tsv").bufferedReader().use { TsvReader(it).readAll() }
*/
class TsvReader(private val reader: Reader) {
private val sb = StringBuilder()
private val eof = (-1).toChar()
@jeiea
jeiea / FlatDictionary.cs
Last active September 1, 2018 10:35
A utility for importing separate xaml files while previewing it in xaml editor
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Markup;
/// <summary>
/// A utility for importing separate xaml files while previewing it in xaml editor.
/// </summary>
// <example>
@jeiea
jeiea / WeakCache.cs
Created July 15, 2018 21:04
Not thread safe..
public class WeakCache<T> where T : class {
private Dictionary<object, WeakReference<T>> Index
= new Dictionary<object, WeakReference<T>>();
public bool TryGetValue(object key, out T val) {
if (Index.TryGetValue(key, out WeakReference<T> weak))
if (weak.TryGetTarget(out T target)) {
val = target;
return true;
}
@jeiea
jeiea / echo-server.js
Created March 25, 2018 09:54
node.js echo server
const net = require('net');
let clients = [];
let server = net.createServer(stream => {
stream.setEncoding('utf8');
clients.push(stream);
let idx = clients.indexOf(stream);
console.log(`client ${idx} connected.`);
stream.on('data', data => {

ํ•˜์Šค์ผˆ์„ C๋งŒํผ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค๊ธฐ: ์—„๊ฒฉํ•จ, ๋А๊ธ‹ํ•จ, ์žฌ๊ท€ ํŒŒํ—ค์น˜๊ธฐ

์›๋ณธ: Write Haskell as fast as C: exploiting strictness, laziness and recursion

์—ญ์ฃผ: ๋ฒˆ์—ญ ์‹œ์ ์—์„œ 10๋…„ ๋œ ๊ธ€... ํ˜„์žฌ์™€๋Š” ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์ƒ๋‹นํ•˜๋‹ค.

์ตœ๊ทผ ๋ฉ”์ผ๋ง ๋ฆฌ์ŠคํŠธ์—์„œ Andrew Coppin์ด ๊ธด double ์‹ค์ˆ˜๊ฐ’ ๋ฆฌ์ŠคํŠธ์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๋Š” "๊น”๋”ํ•˜๊ณ  ์„ ์–ธ์ ์ธ" ์ฝ”๋“œ๊ฐ€ ์„ฑ๋Šฅ์ด ๋‚˜์˜๋‹ค๊ณ  ๋ถˆํ‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

import System.Environment

import Text.Printf