Skip to content

Instantly share code, notes, and snippets.

View dotWasim's full-sized avatar
🏠
Working from home

Wasim Alatrash dotWasim

🏠
Working from home
  • Turkey
View GitHub Profile
// You need to implement a feature that behaves differently based on the user's role. Currently, there are 7 different user roles, and the logic is implemented using a series of if statements with some nested conditions. Here is a simplified version of the code:
function handleUserRole(role) {
if (role === 'admin') {
// Admin-specific logic
} else if (role === 'editor') {
// Editor-specific logic
} else if (role === 'viewer') {
// Viewer-specific logic
if (/* some condition */) {
@dotWasim
dotWasim / gist:2462e32a29ec53b8ba4798f82cc206ee
Created January 4, 2021 10:06
Fix right-to-left ScrollView in SwiftUI
import SwiftUI
struct TestView: View {
var data = ["Test 1", "Test 2"]
var body: some View {
ScrollView(.horizontal) {
HStack{
ForEach(data, id: \.self) { name in
Text(name)
@dotWasim
dotWasim / CountWhere.swift
Created April 4, 2020 11:16
add count(where:) to Sequence protocol
extension Sequence {
func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
try reduce(0) { try predicate($1) ? $0 + 1 : $0 }
}
}
let count = [1,23,4].count(where: { $0 > 2})
print(count)
@dotWasim
dotWasim / UniqueValuesOfArray.swift
Last active February 20, 2020 12:15
Unique values of Array of items
import Foundation
let test = [1,2,3,4,1,2,2,5,20,2,1,7]
// 1- if order of items is important:
extension Sequence where Element: Hashable {
func unique() -> [Element] {
var seen: Set<Element> = []
return filter { seen.insert($0).inserted }
}
@dotWasim
dotWasim / SynchronizedSemaphore.swift
Created February 2, 2020 10:56
Thread-safe counter using DispatchSemaphore
import XCTest
struct SynchronizedSemaphore<Value> {
private let mutex = DispatchSemaphore(value: 1)
private var _value: Value
init(_ value: Value) {
self._value = value
}
@dotWasim
dotWasim / CompareArraysWithCommonProtocol.playground
Created January 22, 2020 12:53
Compare Arrays that implement a common Protocol
import Foundation
import XCTest
protocol Booked {
func isEqual(to other: Booked) -> Bool
}
extension Booked where Self: Equatable{
func isEqual(to other: Booked) -> Bool {
if let other = other as? Self{