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
// 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 */) { |
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
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) |
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
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) |
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
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 } | |
} |
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
import XCTest | |
struct SynchronizedSemaphore<Value> { | |
private let mutex = DispatchSemaphore(value: 1) | |
private var _value: Value | |
init(_ value: Value) { | |
self._value = value | |
} | |
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
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{ |