Skip to content

Instantly share code, notes, and snippets.

@berikv
berikv / ExponentialMovingAverage.swift
Last active December 30, 2022 18:25
Exponential Moving Average
func exponentialMovingAverage(currentAverage: Double, newValue: Double, smoothing: Double) -> Double {
return smoothing * newValue + (1 - smoothing) * currentAverage
}
// Usage:
// var a = 3
// a = exponentialMovingAverage(a, 8, 0.5)
// Swift 2
@berikv
berikv / tictactoe.swift
Last active November 11, 2022 16:16
A TicTacToe game written with SwiftUI, works in Xcode 11.0 beta Playground
import PlaygroundSupport
import SwiftUI
// MARK: - Game
extension TicTacToe.Board {
var hasNonEmptyCells: Bool {
self.flatMap { $0 }
.contains(where: { $0 != .empty })
}
@berikv
berikv / uninstall_xcode.sh
Created April 21, 2022 14:11
Uninstall XCode.app, its caches, CommandLineTools and installation records
#!/bin/sh
set -x
sudo rm -rf /Library/Developer/CommandLineTools/
sudo rm -rf /Applications/Xcode.app
rm -rf ~/Library/Preferences/com.apple.dt.Xcode.plist
rm -rf ~/Library/Caches/com.apple.dt.Xcode
rm -rf ~/Library/Application\ Support/Xcode
rm -rf ~/Library/Developer/
@berikv
berikv / ViewPrint.swift
Created January 13, 2022 16:19
Allow views to print values in ViewBuilders
import SwiftUI
/**
* An @ViewBuilder will only allow statements that result in a View.
* `print(value)` does not result in a view and is not allowed in a @ViewBuilder such as `var body: some View`.
* This view extension allows you print inside a @ViewBuilder.
*
* Usage:
* ```
* struct MyView: View {
* var body: some View {
@berikv
berikv / gifify
Last active December 21, 2021 16:51
Transform a movie file to a gif. Specialised for demo's of Mobile app screen recordings.
#!/bin/bash
FFMPEG=$(command -v ffmpeg)
INFILE=$1
OUTFILE="${INFILE}.gif"
TMPFILE="${INFILE}_gifify_palette.png"
if ! $FFMPEG -L > /dev/null 2>&1; then
echo "Run: brew install ffmpeg"
exit 1
@berikv
berikv / coverage.sh
Last active November 23, 2021 17:20
Coverage generator for Swift Package based projects
#! sh
# Install:
# - Place this file next to the Package.swift in your project
# - Open a terminal and cd to the project directory
# - Run: brew install lcov
# - Run: chmod +x coverage.sh
#
# Usage:
# Run: ./coverage.sh
@berikv
berikv / Retry.swift
Created November 19, 2021 14:53
retry() code block in Swift
/// Retry a block of code until it succeeds, maximum n times
///
/// Usage:
/// ```
/// var failTimes = 3
/// try retry(5) {
/// if failTimes > 0 {
/// failTimes -= 1
/// throw MyError()
/// }
@berikv
berikv / SwiftUIBindings.swift
Created November 10, 2021 13:50
SwiftUI Bindings playground
// Drop this code in an xcode playground for iOS
// Open the canvas to see the view rendered
// Cmd+Enter to Run
import PlaygroundSupport
import SwiftUI
// Wrap your application state in observable objects
// Adhering to ObservableObject is needed for @ObservedObject, @StateObject and @EnvironmentObject
final class Clock: ObservableObject {
@berikv
berikv / metatype.swift
Created October 16, 2021 10:23
Static member cannot be used on protocol metatype
protocol P {}
struct S: P {}
extension P {
static var a: P { S() }
}
let v = P.a
// error: static member 'a' cannot be used on protocol metatype 'P.Protocol'
// let v = P.a
@berikv
berikv / .gitalias.sh
Last active July 20, 2021 11:27
Some convenient and readable git aliases
# Paste these lines into your ~/.bashrc or other shell initialisation script
# Note that for most of these, your gitconfig has to have the aliasses from the .gitconfig in this gist
alias co='git checkout'
alias st='git status'
alias add='git add'
alias commit='git commit'
# Amend anything that is staged