Skip to content

Instantly share code, notes, and snippets.

View dineshba's full-sized avatar
👨‍💻
Trying to be an OSS contributor

Dinesh B dineshba

👨‍💻
Trying to be an OSS contributor
View GitHub Profile
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")
}
}
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)
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
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`
class Dog {
var name: String
var weight: Int
// constructor or initializer
init(name: String, weight: Int) {
self.name = name
self.weight = weight
}
}
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
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? {
@dineshba
dineshba / show_commit.sh
Last active March 23, 2019 02:45
Display what the commit is made of. It will show the tree, blobs and the structure of it.
# 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;
### 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 ###
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