Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / Main.java
Created July 14, 2014 09:09
Retrofit + RxJava in Java
package com.company;
import retrofit.RestAdapter;
import retrofit.http.GET;
import retrofit.http.Path;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
@jooyunghan
jooyunghan / Markov.java
Created July 18, 2014 13:49
Random text generation example (The Practice of Programming)
import java.util.*;
public class Markov {
final static int MAX = 100;
public static void main(String[] args) {
Map<List<String>,List<String>> map = new HashMap<>();
List<String> prefix = Arrays.asList("", "");
@jooyunghan
jooyunghan / markov.hs
Created July 18, 2014 15:39
Random text generation (The Practice of Programming)
import Data.Map
import System.Random
group assoc = fromListWith (++) [(k,[v]) | (k,v) <- assoc]
mapChooseRandom g = zipWith chooseRandom (randoms g)
chooseRandom r list = list !! (r `mod` (length list))
learn str = group $ zip prefices suffices
where prefices = zip w (tail w)
int[][] data = new int[3][3];
private void aiMove() {
Score s = minimax(null, true);
data[s.move.x][s.move.y] = COMPUTER;
}
static class Score {
Point move;
int score;
import qualified Data.Map as M
-- Cell
data Cell = Cross | Naught | Empty deriving (Eq, Show)
to_char Cross = 'X'
to_char Naught = 'O'
to_char Empty = '.'
to_int Cross = 2
@jooyunghan
jooyunghan / Minimax.hs
Created August 1, 2014 23:17
Haskell port of Tic-Tac-Toe example in 'Why Functional Programming Matters'
module Minimax (aimove) where
import Tree
-- Find value for max key from assoc list
maxkey :: (Ord k) => [(k, v)] -> v
maxkey = snd . foldl1 (\(k1,v1) (k2,v2) -> if k1 > k2 then (k1,v1) else (k2,v2))
max' :: (Ord a) => [a] -> a
max' = foldl1 max
@jooyunghan
jooyunghan / AlphaBetaImperative.scala
Created August 5, 2014 09:01
Minimax with Alpha-beta Pruning
class AlphaBetaImperative extends Evaluator {
override def evaluate(tree: Tree): Int = alphabeta(tree, Int.MinValue, Int.MaxValue, true)
def alphabeta(tree: Tree, alpha: Int, beta: Int, maximizing: Boolean): Int = {
tree match {
case leaf @ Leaf(_) => staticEval(leaf)
case node @ Node(subs) =>
if (maximizing) {
var a = alpha
import scala.collection.mutable.ListBuffer
object ListExercises {
object SetA {
// Exercise 1. print all names in list
def printAllNames = {
val names = IndexedSeq("Ben", "Jafar", "Matt", "Priya", "Brian")
for (i <- 0 until names.length) {
println(names(i))
@jooyunghan
jooyunghan / RoseTree.java
Created February 23, 2015 16:22
An example for RoseTree in Java 8
import java.util.List;
import java.util.Arrays;
public class RoseTree<T> {
private final T value;
private final List<RoseTree<T>> children;
public RoseTree(T value, List<RoseTree<T>> children) {
this.value = value;
this.children = children;
@jooyunghan
jooyunghan / gist:7497419caaca7e579357
Created March 13, 2015 09:53
GitHub Repo Searches
var _ = require('lodash');
var github = require('octonode');
var Rx = require('rx');
// repos :: Search -> Options -> Observable Data
// callback style is converted to Promise and then to Observable
function repos(search, options) {
return Rx.Observable.fromPromise(new Promise(function (resolve, reject) {
search.repos(options, function (err, body, header) {