Skip to content

Instantly share code, notes, and snippets.

View sungjk's full-sized avatar
🇰🇷
sup

Jeremy Kim sungjk

🇰🇷
sup
View GitHub Profile
[
{
"key": "FEATURE_NEW_CREATE_POST",
"enabled": false,
"debug": false,
"permission": {
"enabled": false,
"debug": false,
"user_ids": []
},
@sungjk
sungjk / groupedCollect.scala
Created August 10, 2018 15:56
`groupedCollect` helper method for parallelism
object future {
def groupedCollect[A, B](xs: Seq[A], par: Int)(f: A => Future[B]): Future[Seq[B]] = {
val bsF: Future[Seq[B]] = Future.value(Seq.empty[B])
xs.grouped(par).foldLeft(bsF){ case (bsF, group) => {
for {
bs <- bsF
xs <- Future.collect(group.map(f))
} yield bs ++ xs
}}
}
@sungjk
sungjk / groupBy.js
Created July 25, 2018 06:43
groupBy for javascript
function groupBy(xs, key) {
return xs.reduce(function (rv, x) {
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el) {
el.values.push(x);
} else {
rv.push({
key: v,
values: [x]
@sungjk
sungjk / Functional.java
Last active July 9, 2018 05:44
functional programming style helpers for JAVA
public class Functional {
public interface Function3<A,B,C,R> {
R apply(A a, B b, C c) throws Throwable;
}
public static class Either<L, R> {
final public Optional<L> left;
@sungjk
sungjk / exception.java
Created June 22, 2018 02:04
try-catch-finally on Java
public static String lem() {
System.out.println("lem");
return "return from lem";
}
public static String foo() {
int x = 0;
int y = 5;
try {
System.out.println("start try");
@sungjk
sungjk / dijkstra.scala
Last active June 6, 2018 08:32
Dijkstra algorithm
object GlobalGraph {
type NodeId = Int
type Distance = Int
type Nodes = Set[Node]
type Path = (Nodes, Distance)
type Graph = Map[Edge, Distance]
case class Node(id: NodeId, dist: Distance = Int.MaxValue, prevNode: Option[Node] = None)
case class Edge(from: NodeId, to: NodeId)
}
@sungjk
sungjk / KeyValueStore.sol
Created May 31, 2018 06:21
KeyValueStore.sol
contract KeyValueStore {
uint256 keyIndex;
struct values {
string value1;
string value2;
}
mapping (uint256 => values) Obj;
@sungjk
sungjk / Par.scala
Last active June 16, 2018 12:21
Par.scala
class ExecutorService {
def submit[A](a: Callable[A]): Future[A]
}
trait Callable[A] { def call: A }
trait Future[A] {
def get: A
def get(timeout: Long, unit: TimeUnit): A
def cancel(evenIfRunning: Boolean): Boolean
@sungjk
sungjk / MonadTransformer.scala
Last active April 30, 2018 09:06
MonadTransformer
import scala.concurrent.Future
import scala.language.higherKinds
sealed trait Monad[T[_]] {
def map[A, B](value: T[A])(f: A => B): T[B]
def flatMap[A, B](value: T[A])(f: A => T[B]): T[B]
def pure[A](x: A): T[A]
}