Skip to content

Instantly share code, notes, and snippets.

@alexnikol
alexnikol / .swift
Last active Jan 25, 2021
Decoding JSON with endless nesting
View .swift
import Foundation
let JSON = """
[{
"sub_elements": [{
"sub_elements": [{
"id": "4315FAC2-0809-478C-BED8-54F28B0A9A57",
"name": "Shape 1"
}, {
"id": "D2985D5B-B98A-4549-A4ED-3742583A2532",
@alexnikol
alexnikol / .swift
Created Aug 17, 2020
Generic way to solve pagination duplications
View .swift
class Pagination<Element: Decodable>: Decodable {
var list: [Element]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case list, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created Aug 17, 2020
Pagination duplicates problem
View .swift
class Pagination: Decodable {
var pets: [Pet]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case pets, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created Aug 17, 2020
Default Pagination Realization
View .swift
class Pagination: Decodable {
var cars: [Car]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case cars, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created Aug 17, 2020
Generic way code after refactoring
View .swift
func handleDecoding<Element: Decodable>(responseData: Data,
responseType: Element.Type,
completion: @escaping (_ apiResponse: Element?, _ error: String?) -> Void) {
do {
let apiResponse = try JSONDecoder().decode(responseType, from: responseData)
completion(apiResponse, nil)
} catch {
completion(nil, "Network Error")
}
}
@alexnikol
alexnikol / .swift
Last active Aug 17, 2020
Problematic code before the Generics magic
View .swift
// PetNetworkManager
func handleDecoding(responseData: Data,
completion: @escaping (_ apiResponse: Pet?, _ error: String?) -> Void) {
do {
let apiResponse = try JSONDecoder().decode(Pet.self, from: responseData)
completion(apiResponse, nil)
} catch {
completion(nil, "Network Error")
}
}
@alexnikol
alexnikol / .py
Created Aug 12, 2020
Insert a node at the head of a linked list. Testing
View .py
# RESULT
node1 = SinglyLinkedListNode(1)
node2 = SinglyLinkedListNode(2)
node3 = SinglyLinkedListNode(3)
node1.next = node2
node2.next = node3
result = insertNodeAtHead(node1, 33)
current_node = result
@alexnikol
alexnikol / .py
Last active Aug 12, 2020
Insert a node at the head of a linked list. Realization
View .py
from linked_list import SinglyLinkedListNode
def insertNodeAtHead(llist, data):
node = SinglyLinkedListNode(data)
node.next = llist
return node
@alexnikol
alexnikol / .py
Created Aug 10, 2020
Insert a Node at the Tail of a Linked List. Testing
View .py
# RESULT
node1 = SinglyLinkedListNode(1)
node2 = SinglyLinkedListNode(2)
node3 = SinglyLinkedListNode(3)
node1.next = node2
node2.next = node3
result = insertNodeAtTail(node1, 34)
@alexnikol
alexnikol / .py
Last active Aug 10, 2020
Insert a Node at the Tail of a Linked List. Realization
View .py
from linked_list import SinglyLinkedListNode
def insertNodeAtTail(head, data):
if head is None:
return SinglyLinkedListNode(data)
else:
current_node = head
while current_node.next is not None:
current_node = current_node.next