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
if case .sugar = sugarcane.taste { // if sugarcane.taste is `.sugar` then | |
print("yummmmy sugarcane") // enters into if | |
} | |
if case .salty(let value) = frenchFries.taste { // frenchFries.taste is .salty(10), so it matches | |
if value < 10 { // the case and assign the value 10 to `value` | |
print("manageable") // we can use the `value` inside the `if case` | |
} else { | |
print("impossible") | |
} | |
} |
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
struct ComputerLab { // Computer Lab contains list of laptops. | |
let laptops: [Laptop] | |
} | |
// we want to get the list of mac laptops | |
// Traditional way | |
var macLaptops: [Laptop] = [] | |
for laptop in computerLab.laptops { | |
if laptop.name.contains("Mac") { | |
macLaptops.append(laptop) |
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 fanNames = computerLab.laptops | |
.filter({laptop in !laptop.name.contains("Mac") }) | |
.filter({laptop in laptop.fan != nil}) | |
.map({laptop in laptop.fan!.name}) | |
// Simplified | |
let fanNames = computerLab.laptops | |
.filter({laptop in !laptop.name.contains("Mac") }) | |
.compactMap({laptop in laptop.fan?.name}) //compactMap will filter out nil values by default |
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
var anyValue: Any // If we don't know the type at compile time, | |
// we can use `Any`. It is mostly used for data | |
// coming from network which can be type casted as below. | |
anyValue = "string value" // assinging string value | |
anyValue is String // true | |
// type checking using `is` operator | |
if let stringValue = anyValue as? String { // if anyValue is castable to String, it will | |
print(stringValue.uppercased()) // store in stringValue and enter into `if-let` |
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
class Dog { | |
var name: String | |
var weight: Int | |
// constructor or initializer | |
init(name: String, weight: Int) { | |
self.name = name | |
self.weight = weight | |
} | |
} |
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
struct Cherry: Eatable { | |
var taste: Taste = Taste.sweet | |
func isBitter() -> Bool { | |
return self.taste == .bitter | |
} | |
} | |
let 🍒 = Cherry() // literally we can have variable name like this🤓 | |
struct Chilly: Eatable { | |
var taste: Taste = Taste.spicy |
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
var optionalValue: Int? | |
if let value = optionalValue { // unwraps optionalValue and assigns | |
// it to variable named value | |
print(value) // type of value = Int | |
else { | |
print("not able to unwrap optionalValue as it is nil") | |
} | |
//Let's rewrite the sum function using if-let | |
func sum(_ optionalAugend: Int?, _ optionalAddend: Int?) -> Int? { |
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
# usage show_commit commit-sha | |
# usage show_commit HEAD | |
#!/bin/sh | |
show_tree() { | |
spacing=$2 | |
printf "$spacing showing tree $1 \n" | |
for one in $(git cat-file -p $1 | awk '{printf $2"--"$3 "\n"}'); do | |
printf "$spacing $one \n" | |
done; |
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
### custom shell prompt ### | |
parse_git_branch() { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | |
} | |
PS1='\[\e[31;47m\]$(kubectl config current-context)\[\e[m\]\[\e[36;47m\]$(kubectl config view --minify --output 'jsonpath={..namespace}')\[\e[m\]\w\[\033[32m\]$(parse_git_branch)\[\033[00m\] \$ ' | |
### cd dir interactively ### | |
alias godir='cd $(find ~/projects -type d -maxdepth 2 | fzf)' | |
### load tmux session interactively ### |
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
Learning | |
- Technical | |
- Use stderr and stdout properly (60% content for this 5w1h) (why do we need stderr, redirection, identify) | |
- Use feature toggles | |
- Write modular code in pkg dir so that anybody can import and use it (Eg helm, kubectl) | |
- Easy dev setup (Makefile) | |
- Logging at different log level (--debug flag) | |
- 12 factor app (file, env, flag) | |
- hyperfine for benchmark | |
- NonTechnical |