This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DataTableExtensions | |
{ | |
public static IEnumerable<dynamic> ToExpandoObjectList(this DataTable self) | |
{ | |
var result = new List<dynamic>(self.Rows.Count); | |
foreach (var row in self.Rows.OfType<DataRow>()) | |
{ | |
var expando = new ExpandoObject() as IDictionary<string, object>; | |
foreach (var col in row.Table.Columns.OfType<DataColumn>()) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%raw( | |
"/* | |
* this js function will work under both [string] and [float] | |
*/ | |
function add (x,y){ | |
return x + y; | |
}") | |
type rec t<_> = | String : t<string> | Float : t<float> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
To implement a trait on a type, the trait or the type has to be local to the code you work on. It is called the orphan rule. | |
To get around this restriction, we can use the newtype pattern which involves creating a new type in a tuple struct. | |
Both Vec<T> and Display trait are from the standard library and neither is local to our code. | |
We are not able to implement Display trait to Vec<T>. But we can construct a wrapper type holding an instance of Vec<T> and implement Display trait on it. | |
In the following example, VecWrapper<T> struct wraps Vec<T> and implements Display trait. | |
To get all the methods of Vec<T> available to VecWrapper, Deref trait is implemented on it. | |
ref: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html?search=borrow%20and%20asref | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tailwindColors = { | |
foo: { | |
50: '#f9fafb', | |
100: '#f3f4f6', | |
200: '#e5e7eb', | |
300: '#d1d5db', | |
400: '#9ca3af', | |
500: '#6b7280', | |
600: '#4b5563', | |
700: '#374151', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(result_map_or_else)] | |
extern crate num_cpus; | |
extern crate reqwest; | |
extern crate threadpool; | |
use std::error::Error; | |
use std::thread; | |
use threadpool::ThreadPool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Animal { | |
fn speak(&self); | |
} | |
#[derive(Debug)] | |
struct Dog { | |
name: String, | |
age: u8, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(start)] | |
#![no_std] | |
extern crate libc; | |
use core::panic::PanicInfo; | |
use libc::{c_char, c_int, c_void, size_t}; | |
extern "C" { | |
fn malloc(size: size_t) -> *mut c_void; | |
fn free(p: *mut c_void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::io::prelude::*; | |
fn main() { | |
loop { | |
let r = get_input("Enter the first number: ") | |
.and_then(|x| get_input("Enter the second number: ") | |
.and_then(|y| get_input("Enter the third number: ") | |
.and_then(|z| Ok(x + y + z)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::borrow::BorrowMut; | |
pub trait OptionExt<T> { | |
fn fold<S, F>(&self, state: S, folder: F) -> S | |
where | |
F: FnOnce(S, &T) -> S; | |
} | |
impl<T> OptionExt<T> for Option<T> { | |
fn fold<S, F>(&self, state: S, folder: F) -> S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mut v = vec![1, 2, 3, 4, 5]; | |
let r = v | |
.get_mut(1..=2) | |
.map(|x| x.iter_mut().for_each(|y| *y *= 10)); | |
if r.is_some() { | |
println!("Succeed : {:?}", v); | |
} else { | |
println!("Not Succeed. Do something here."); | |
} |
NewerOlder