Merge branch 'master' of https://github.com/chubin/cheat.sheets
commit
54ba5234a4
@ -0,0 +1,8 @@
|
||||
# Forkbomb
|
||||
# DO NOT RUN UNLESS YOU WANT YOUR COMPUTER TO CRASH
|
||||
#
|
||||
# This defines a function named ':' that calls itself, pipes its output back to
|
||||
# itself, and runs that in the background. the ';:' at the end terminates the
|
||||
# function definition and then calls the function to begin the chain reaction.
|
||||
# A very nasty trick, indeed.
|
||||
:(){ :|: & };:
|
@ -0,0 +1,27 @@
|
||||
(*
|
||||
* Generate a Taylor series expansion of cos(x) using x, expanding about x = 0 and
|
||||
* continuing until a term with x^6. Taylor series about x = 0 are called Maclaurin
|
||||
* series.
|
||||
*)
|
||||
|
||||
Series[Cos[x], {x, 0, 6}]
|
||||
1 - x^2/2 + x^4/24 - x^6/720 + O[x]^7
|
||||
|
||||
(*
|
||||
* The 'O[x]^7' just represents the rest of the series, which we don't care about.
|
||||
* If you don't want it displayed, just wrap the call to Series inside a call to
|
||||
* Normal: Normal[Series[...]]. This is useful for plotting series.
|
||||
*
|
||||
* Here's the same function, but expanded about a different point, x = 3pi/2:
|
||||
*)
|
||||
|
||||
Series[Cos[x], {x, 3 Pi/2, 6}]
|
||||
(x-3pi/2) - 1/6*(x-3pi/2)^3 + 1/120*(x-3pi/2)^5 + O[x-3pi/2]^7
|
||||
|
||||
(*
|
||||
* When plotting series, remember to wrap the function in both a call to Normal AND
|
||||
* a call to Evaluate: this strips the extra term mentioned previously and tells
|
||||
* Mathematica to actually evaluate the function rather than hold it as an expression.
|
||||
*)
|
||||
|
||||
Plot[Evaluate[Normal[Series[Cos[x], {x, 0, 6}]]], {x, 0, 1}]
|
@ -0,0 +1,33 @@
|
||||
# Similar to a switch statement if you've used C/C++/Java, but Ruby's
|
||||
# is a bit more powerful.
|
||||
|
||||
# Basic value tests
|
||||
num = 5
|
||||
case num
|
||||
when 1
|
||||
puts "it's one"
|
||||
when 3
|
||||
puts "wow, it's three"
|
||||
when 5
|
||||
puts "five golden rings"
|
||||
end
|
||||
|
||||
# Ranges
|
||||
num = 25
|
||||
case num
|
||||
when 1..10
|
||||
puts "between one and ten"
|
||||
when 20..30
|
||||
puts "between twenty and thirty"
|
||||
else
|
||||
puts "must be between ten and twenty"
|
||||
end
|
||||
|
||||
# Regular expressions
|
||||
str = "doe, a deer"
|
||||
case str
|
||||
when /food/
|
||||
puts "i'm hungry"
|
||||
when /doe/
|
||||
puts "#{str}, a female deer"
|
||||
end
|
@ -0,0 +1,49 @@
|
||||
# Basic iteration
|
||||
|
||||
# Good ol' for-loop
|
||||
for i in [1, 2, 3]
|
||||
puts i
|
||||
end
|
||||
|
||||
# Typically we don't use for-loops in Ruby, though.
|
||||
# Rubyists tend to prefer .each
|
||||
[1, 2, 3].each { |i| puts i }
|
||||
|
||||
# Using .each looks neater and it allows for powerful chaining
|
||||
# of enumerable types.
|
||||
#
|
||||
# This example maps the array to [2, 4, 6] and prints each element.
|
||||
[1, 2, 3].map { |i| i * 2 }.each { |i| puts i }
|
||||
#
|
||||
# Another kind of loop with specified iterations
|
||||
10.times { puts 'test' }
|
||||
10.times do |n|
|
||||
puts "number #{n}"
|
||||
end
|
||||
|
||||
# Conditionals
|
||||
if 2 * 4 == 8
|
||||
puts 'thank goodness'
|
||||
else
|
||||
puts 'error between keyboard and chair'
|
||||
end
|
||||
|
||||
# Add an extra condition
|
||||
if 1 == 0
|
||||
puts 'oh no'
|
||||
elsif 1 == 1
|
||||
puts "that's better"
|
||||
else
|
||||
puts 'cut it out'
|
||||
end
|
||||
|
||||
# unless means 'if not'
|
||||
unless 1 == 0
|
||||
puts 'everything is ok'
|
||||
end
|
||||
|
||||
# If the condition is short you can put it on one line.
|
||||
happiness = 0 if 'my life'.empty?
|
||||
|
||||
# Or use ternary operator
|
||||
happiness = 'my life'.empty? ? 0 : 100
|
@ -0,0 +1,27 @@
|
||||
// Basic threading
|
||||
use std::thread;
|
||||
//
|
||||
fn main() {
|
||||
// Spawn a new thread to do some work
|
||||
let t = thread::spawn(|| {
|
||||
println!("In a new thread!");
|
||||
});
|
||||
|
||||
// Wait for execution of spawned thread to join back up with main thread
|
||||
let result = t.join();
|
||||
|
||||
// Create a bunch of threads, stored in a Vec
|
||||
let mut threads = vec![];
|
||||
for i in 0..10 {
|
||||
threads.push(thread::spawn(|| {
|
||||
println!("Doing some work!");
|
||||
}));
|
||||
}
|
||||
|
||||
// Wait for all of those to finish
|
||||
for t in threads {
|
||||
let result = t.join();
|
||||
}
|
||||
}
|
||||
// Check out rayon https://docs.rs/rayon/ for more advanced tools like parallel
|
||||
// iterators and sort algorithms.
|
Loading…
Reference in New Issue