Skip to content

Instantly share code, notes, and snippets.

@snowmantw
snowmantw / gist:5419014
Last active December 16, 2015 10:19
Type failed while traits in other traits.
trait Printable
{
fn print(&self);
}
impl Printable for int
{
fn print(&self) { io::println(fmt!("Trait Print: %d", *self)) }
}
@snowmantw
snowmantw / gist:5419166
Created April 19, 2013 09:21
Log for solving my question of traits in traits.
as I read, the `'b` seems named lifetime..
17:10 snowmantw but I just can't figure it out...
17:10 Yurume snowmantw: `pcs[0]` syntax is directly connected to `core:ps::Index` trait.
17:10 Heather jdm it's sad
17:10 Yurume so you need `S:Seq<T>+Index` for using `pcs[0]`
17:10 Heather jdm so I can't get it on windows anyhow?
17:10 jdm Heather: sorry. we're a very small team with limited resources at the moment.
17:11 snowmantw #Yurume I saw it used as Copy in http://static.rust-lang.org/doc/tutorial.html
17:11 Heather maybe cygwin
17:11 jdm Heather: if you want to try to get the windows port working, we would accept pull requests.
@snowmantw
snowmantw / gist:5424589
Created April 20, 2013 03:18
Rust index usage?

Codes from 'src/libstd/bitv.rs' in Rust compiler git repo.

Key:

// NOTE cannot use bitv[offset] until snapshot
bitv.index(&offset) as u8 << (7 - bit) 

So it seems Index<A,B> currently can't support bitv[T] ?

@snowmantw
snowmantw / gist:5449365
Created April 24, 2013 03:24
flatten callbacks without any library
function main()
{
(input
(foo
(bar
("some_setting", 3.14,
output))))
}
function foo(cb)
@snowmantw
snowmantw / gist:5464738
Created April 26, 2013 02:29
Execute concurrently on list.
var http = require("http")
function mkAction(url, cb)
{
return function()
{
http.get(url, function(res) {
res.on("data", function(chunk) {
cb(chunk)
--langdef=javascript
--langmap=javascript:.js
--regex-javascript=/^([ \t]*[a-zA-Z$_]+)\.([a-zA-Z$_]+)\(([^\)]*)\)/\1.\2 \3 /c, chain, chains/
--regex-javascript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Object\(/\1/o,object/
--regex-javascript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/o,object/
--regex-javascript=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/f,function/
--regex-javascript=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\([^\]\)]*\)/\1/f,function/
--regex-javascript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Array\(/\1/a,array/
--regex-javascript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/a,array/

Anonymous Function

  • TypeError if anonymous functions without semicolons:

    (function() { console.log('1') })() // ;

@snowmantw
snowmantw / gist:5661556
Created May 28, 2013 09:17
infinite generator
function r()
{
var _r = function(){}
_r.result = 0
_r.done = function()
{
return _r.result
}
_r.call = function()
{
#!/bin/bash
TPATH=$1
URL_UNDERSCORE="http://underscorejs.org/underscore-min.js"
URL_JQUERY="http://code.jquery.com/jquery-2.0.2.min.js"
GURL_FLUORINE="git://github.com/snowmantw/Fluorine.git"
PORT="8090"
echo "Start to creat site: $TPATH"
[:program, [:modules, [:list, [:module, [:name, :main], [:list], [:list, [:fn, [:name, :main], [:list], [:stmts, [:stmts, [:stmts, [:stmts, [:stmts, [:stmts, [:noop], [:write, :SMC_PRIVATE_A, [:str, "How much money do you have?\n"]]], [:write, :SMC_PRIVATE_B, [:str, "How much money do you have?\n"]]], [:read, [:idrefer, [:name, :asset_a], [:list]]]], [:read, [:idrefer, [:name, :asset_b], [:list]]]], [:asn, [:id, [:name, :empty], [:accessor]], [:call, [:id, [:name, :empty_fn], [:accessor, [:id, [:name, :main], [:accessor]]]], [:list, [:id, [:name, :empty], [:accessor]]]]]], [:return, [:call, [:id, [:name, :compare], [:accessor, [:id, [:name, :main], [:accessor]]]], [:list, [:id, [:name, :asset_a], [:accessor]], [:id, [:name, :asset_b], [:accessor]]]]]], [:list, [:return, [:call, [:id, [:name, :compare], [:accessor, [:id, [:name, :main], [:accessor]]]], [:list, [:id, [:name, :asset_a], [:accessor]], [:id, [:name, :asset_b], [:accessor]]]]]], [:sig, [:id, [:name, :main], [:accessor]], [:list, [:sigdecl, :SMC_SHA