Skip to content

Instantly share code, notes, and snippets.

@hyone
hyone / Cargo.toml
Created July 2, 2017 11:11
Rust で並列HTTP Request (非同期IO版) ref: http://qiita.com/hyone/items/7f448c46611d9b19e643
[package]
name = "http_request2"
version = "0.1.0"
[dependencies]
futures = "*"
hyper = "0.11.0"
tokio-core = "*"
[package]
name = "http_request1"
version = "0.1.0"
[dependencies]
hyper = "*"
@hyone
hyone / gitfile1.rs
Last active October 15, 2016 07:18
open file with std::error::Error
use std::error::Error as StdError;
use std::env;
use std::fmt;
use std::fs::File;
use std::io::Error as ioError;
use std::io::prelude::*;
use std::path::Path;
macro_rules! eprintln {
@hyone
hyone / gist:02b81a2a176ca0ada59a3343b6787b79
Last active October 9, 2016 07:53
Practice: lifetime qualifier added version of sample code at http://rustbyexample.com/trait.html
struct Sheep<'a> { naked: bool, name: &'a str }
trait Animal<'a> {
fn new(name: &'a str) -> Self;
fn name(&self) -> &str;
fn noise(&self) -> &str;
fn talk(&self) {
println!("{} says {}", self.name(), self.noise());
@hyone
hyone / gist:d6018ee1ac8f9496fed839f481eb59d6
Last active April 2, 2022 08:15
Practice: implement `Display` trait for `slice` wrapper
use std::fmt::{ Debug, Display, Formatter, Result };
use std::string::ToString;
#[derive(Debug)]
struct Slice<'a, T: 'a> {
data: &'a [T]
}
impl <'a, T: ToString> Display for Slice<'a, T> {
fn fmt(&self, f: &mut Formatter) -> Result {
@hyone
hyone / file0.txt
Created April 7, 2016 10:46
Ecto.Query を SQL に変換する ref: http://qiita.com/hyone/items/06fd744c54d701617a7d
# iex -S mix
iex(1)> import Ecto.Query
nil
iex(2)> import Ecto.Adapters.SQL
nil
iex(3)> user_id = 2
2
iex(4)> q = from u in SampleApp.User, join: fu in assoc(u, :relationships), where: u.id == ^user_id, select: fu.id
@hyone
hyone / gist:8205717
Created January 1, 2014 06:46
ruby koans: about_scoring_project.rb
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used to calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
# number. (e.g. three fives is 500 points).
@hyone
hyone / gist:6128565
Last active December 20, 2015 12:09
Number of binary trees have N leaves with logging by Writer Monad #2 ( see: http://hyone.hatenablog.com/entry/20120803/1343996582 )
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad.List
import Control.Monad.Trans (lift)
import Control.Monad.Writer
splites :: Int -> [(Int, Int)]
splites n = [ (x, n - x) | x <- [1..n-1] ]
liftList :: Monad m => [a] -> ListT m a
@hyone
hyone / HList.hs
Last active December 10, 2015 04:08
Typed Heterogeneous Lists with DataKinds language extension
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module HList where
@hyone
hyone / gist:3994646
Created November 1, 2012 16:10
Example code to implement instances for Singlton types
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Example code to implement instances for Singlton types introduced GHC 7.6.1