Skip to content

Instantly share code, notes, and snippets.

main = undefined
getAllSums x = [(l,r)| l <- partial_nat, r <- partial_nat, l + r == x ]
where partial_nat = [1..x]
use std::string::String;
trait MyToStr {
fn my_to_str(s: &Self) -> String;
}
struct Foo {
name : String
}
impl MyToStr for Foo {
fn my_to_str(s : &Foo) -> String {
fn compose<A, B, C>(f : |B| -> C,
g : |A| -> B)
-> |A| -> C{
|x| f(g(x))
}
fn main(){
}
trait Functor<A>{
fn fmap<B>(f : |A| -> B, v : A) -> B;
}
impl Functor<i32> for i32 {
fn fmap<B>(f : |i32| -> B, v : i32) -> B {
f(v)
}
}
fn main(){
trait Functor<A,B,C>{
fn fmap(&self, f : |&A| -> B) -> C;
}
impl<B> Functor<i32,B,B> for i32 {
fn fmap(&self,f : |&i32| -> B) -> B {
f(self)
}
}
impl<A,B> Functor<A,B,Vec<B>> for Vec<A> {
fn fmap(&self, f : |&A| -> B) -> Vec<B>{
# Define a daysBetweenDates procedure that would produce the
# correct output if there was a correct nextDay procedure.
#
# Note that this will NOT produce correct ouptuts yet, since
# our nextDay procedure assumes all months have 30 days
# (hence a year is 360 days, instead of 365).
#
def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
if day < 30:
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
#include <functional>
#include <iostream>
template<typename T>
class MaybePtr{
T* ptr;
public:
MaybePtr(T* p) : ptr(p) {}
template <typename F,typename R = std::result_of<F(T*)>::type>
R Get(F access,R default_value){
struct Foo{
value: i32
}
impl Foo{
fn get_and_change_value(&mut self) -> i32{
let v = self.value;
self.value = 42;
v
}
}
#![feature(macro_rules)]
use std::comm;
macro_rules! with{
($x:[expr]) =>{
$x.i
}
}
trait ReactiveContainer<T> {
fn set_value(&mut self,value : T);