Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
bdsaglam / SeparableConv2d.py
Last active January 31, 2025 09:38
Separable 2D Convolution PyTorch
class SeparableConv2d(torch.nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size=3,
stride=1,
padding=0,
dilation=1,
bias=True,
padding_mode='zeros',
@bdsaglam
bdsaglam / DepthwiseConv2d.py
Last active June 26, 2022 15:13
Depthwise 2D Convolution PyTorch
import torch
class DepthwiseConv2d(torch.nn.Conv2d):
def __init__(self,
in_channels,
depth_multiplier=1,
kernel_size=3,
stride=1,
padding=0,
dilation=1,
@bdsaglam
bdsaglam / ArrayArgMaxMin.swift
Created March 29, 2020 08:56
Swift array argmax and argmin
extension Array where Element: Comparable {
func argmax() -> Index? {
return indices.max(by: { self[$0] < self[$1] })
}
func argmin() -> Index? {
return indices.min(by: { self[$0] < self[$1] })
}
}
@bdsaglam
bdsaglam / TakeWhileInclusive.swift
Created January 23, 2020 09:02
RxSwift takeWhileInclusive operator
extension ObservableType {
func takeWhileInclusive(predicate: @escaping (Element) throws -> Bool) -> Observable<Element> {
let source = self.share()
let pass = source.map(predicate).startWith(true)
return Observable.zip(source, pass){ ($0, $1) }
.takeWhile { $0.1 }
.map { $0.0 }
}
}
@bdsaglam
bdsaglam / ArrayUtilities.swift
Created January 16, 2020 11:59
Useful array extensions
extension Array {
func group<Key: Hashable>(by transform: (Element) -> Key) -> [Key: [Element]]{
return self.reduce(into: [:]) { dict, current -> () in
let key = transform(current)
var bucket: [Element] = dict[key] ?? []
bucket.append(current)
dict[key] = bucket
}
}
}
@bdsaglam
bdsaglam / SortingUtilities.swift
Last active August 12, 2020 10:39
Swift Sorting Utils
/**
According to the documentation of Swift's standard library, areInIncreasingOrder closure argument of sorted method of sequence type must hold some properties. One of them is that areInIncreasingOrder(a,a) must be false.
From documentation,
"areInIncreasingOrder: A predicate that returns `true` if its first argument should be ordered before its second argument ..."
Swift can check equality by applying this predicate twice with arguments swapped. If it gives false in both case, then elements are equal.
*/
enum Comparison {
case less, equal, greater
@bdsaglam
bdsaglam / partition.swift
Created December 31, 2019 08:14
Array partition in Swift
// from this stackoverflow post
// https://stackoverflow.com/a/48272567/6641096
extension Array {
public func stablePartition(by condition: (Element) throws -> Bool) rethrows -> ([Element], [Element]) {
var indexes = Set<Int>()
for (index, element) in self.enumerated() {
if try condition(element) {
indexes.insert(index)
}
@bdsaglam
bdsaglam / WeakRef.swift
Created December 20, 2019 08:50
Weak reference container in Swift
public final class WeakRef<T: AnyObject> {
weak var object: T?
init(_ object: T) {
self.object = object
}
}
@bdsaglam
bdsaglam / LiveData.swift
Created December 20, 2019 08:46
LiveData in Swift
import Foundation
/**
viewModel.message.observe(owner: self, Observer { (message: String)->() in
DispatchQueue.main.async {
self.label.text = message
}
DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.seconds(1)) {
self.label.text = "tick: \(self.viewModel.ticker.value)"
}
})
@bdsaglam
bdsaglam / filterWithLatestFrom.swift
Last active December 19, 2019 08:31
RxSwift extension for filtering a sequence with another
import RxSwift
extension ObservableType {
func filterWithLatestFrom<Source: ObservableType>(_ second: Source) -> Observable<Element> where Source.Element == Bool {
return self.withLatestFrom(second) { ($0, $1) }
.filter { $0.1 }
.map { $0.0 }
}
}