Skip to content

Instantly share code, notes, and snippets.

@hyone
hyone / gist:1705182
Created January 30, 2012 16:10
Check expression context
use v5.14;
use warnings;
sub hoge {
say do {
given (wantarray) {
when (undef) { "void" }
when (0) { "scalar" }
when (1) { "list" }
}
@hyone
hyone / gist:1711460
Created January 31, 2012 16:33
Multiple async HTTP requests by http.async.client
;; (defproject async-test1 "1.0.0-SNAPSHOT"
;; :description "FIXME: write description"
;; :dependencies [[org.clojure/clojure "1.3.0"]
;; [http.async.client "0.4.0"]]
;; :dev-dependencies [[swank-clojure "1.3.4"]])
(ns async-test1.core
(:require [http.async.client :as client]
[http.async.client.request :as request]))
@hyone
hyone / gist:1835356
Created February 15, 2012 12:24
Multiple async HTTP requests by aleph
;; (defproject async-test2 "1.0.0-SNAPSHOT"
;; :description "FIXME: write description"
;; :dependencies [[org.clojure/clojure "1.3.0"]
;; [aleph "0.2.1-alpha1"]])
(ns async-test2.core
(:use lamina.core
aleph.http))
@hyone
hyone / gist:2707327
Created May 16, 2012 04:13
Inverse FizzBuzz by Clojure
;; Inverse Fizzbuzz - just another scala quant - http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html
;; UPDATED 2012/05/20: This code is wrong, because this only finds the index of the first sub sequence of fizzbuzz sequence.
(ns inverse-fizzbuzz.core)
(defn fizzbuzz []
(map #(cond (zero? (mod % 15)) "fizzbuzz"
(zero? (mod % 3)) "fizz"
(zero? (mod % 5)) "buzz"
@hyone
hyone / gist:2751567
Created May 20, 2012 05:31
Inverse FizzBuzz by Clojure #2
;; Inverse Fizzbuzz - just another scala quant - http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html
(ns inverse-fizzbuzz.core
(:use [clojure.test :only (deftest are)]))
(defn unzip [coll]
(if-not (empty? coll)
(apply map list coll)))
@hyone
hyone / gist:2776665
Created May 23, 2012 17:54
Inverse FizzBuzz by Scala
object InverseFizzBuzz extends App {
implicit def makeSeqSafer[A](xs: Seq[A]) = new {
def safeMinBy[B](f: A => B)(implicit cmp: Ordering[B]) =
if (xs.nonEmpty) Some(xs.minBy(f)) else None
}
def solve(words: Seq[String]): Option[(Int, Int)] = {
List(3,5,6,9,10,12,15).flatMap { x =>
val (subseq, pos) = Stream.from(x).collect {
@hyone
hyone / gist:3164085
Created July 23, 2012 15:00
Scraping Twitter API XML
module Main where
import Control.Applicative
import Control.Monad
import Data.Maybe (fromJust, isJust)
import Network.Curl (curlGetString, URLString)
import qualified Text.XML.Light as XML
url = "http://search.twitter.com/search.atom?q=haskell&rpp=10"
@hyone
hyone / gist:3215897
Created July 31, 2012 10:24
split a list into sublists that have N items
import Control.Monad.State
splitEvery :: Int -> [a] -> [[a]]
splitEvery n = takeWhile (not . null) . evalState (sequence $ repeat (state (splitAt n)))
{-
ghci> splitEvery 5 [1..27]
[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25],[26,27]]
ghci> splitEvery 5 []
[]
@hyone
hyone / gist:3238199
Created August 2, 2012 16:05
Multiple async HTTP requests by using http.async.client callback API
;; (defproject async-test2 "0.1.0-SNAPSHOT"
;; :description "FIXME: write description"
;; :url "http://example.com/FIXME"
;; :license {:name "Eclipse Public License"
;; :url "http://www.eclipse.org/legal/epl-v10.html"}
;; :dependencies [[org.clojure/clojure "1.4.0"]
;; [http.async.client "0.4.5"]])
(ns async-test2.core
@hyone
hyone / gist:3246701
Created August 3, 2012 11:09
Number of binary trees have N leaves
splites :: Int -> [(Int, Int)]
splites n = [ (x, n - x) | x <- [1..n-1] ]
count :: Int -> Int
count 1 = 1
count n = sum $ do
(i, j) <- splites n
return $ count i * count j