Skip to content

Instantly share code, notes, and snippets.

View WhiteHyun's full-sized avatar

SeungHyun Hong WhiteHyun

View GitHub Profile
@WhiteHyun
WhiteHyun / DTOMacro.swift
Created August 10, 2024 23:39
DTO 매크로
public struct DTOMacro: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let structDecl = declaration as? StructDeclSyntax
else {
return []
}
@WhiteHyun
WhiteHyun / PropertyMacro.swift
Created August 10, 2024 23:07
매크로를 두 번 감싸는 걸 방지하는 코드
// MARK: - PropertyMacro
public struct PropertyMacro: AccessorMacro {
public static func expansion(
of node: AttributeSyntax,
providingAccessorsOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [AccessorDeclSyntax] {
guard let varDecl = declaration.as(VariableDeclSyntax.self) else {
@WhiteHyun
WhiteHyun / StructMacroDumpOutput.txt
Created August 10, 2024 16:52
Dumped StructDeclSyntax
StructDeclSyntax
├─attributes: AttributeListSyntax
│ ╰─[0]: AttributeSyntax
│ ├─atSign: atSign
│ ╰─attributeName: IdentifierTypeSyntax
│ ╰─name: identifier("DTO")
├─modifiers: DeclModifierListSyntax
├─structKeyword: keyword(SwiftSyntax.Keyword.struct)
├─name: identifier("TestModel")
╰─memberBlock: MemberBlockSyntax
@WhiteHyun
WhiteHyun / 2191. Sort the Jumbled Numbers.swift
Created July 30, 2024 15:00
2191. Sort the Jumbled Numbers.swift using recursion
final class LeetCode2191 {
func sortJumbled(_ mapping: [Int], _ nums: [Int]) -> [Int] {
zip(nums, nums.map { mapped(mapping, $0) })
.sorted { $0.1 < $1.1 }
.map(\.0)
}
private func mapped(_ mapping: [Int], _ number: Int, _ base: Int = 1) -> Int {
if number < 10 {
base * mapping[number % 10]
@WhiteHyun
WhiteHyun / 2191. Sort the Jumbled Numbers.swift
Last active July 30, 2024 14:35
LeetCode - 2191. Sort the Jumbled Numbers.swift
final class LeetCode2191 {
typealias Item = (original: Int, mapped: Int)
func sortJumbled(_ mapping: [Int], _ nums: [Int]) -> [Int] {
nums
.map { convert($0, using: mapping) }
.sorted {
$0.mapped < $1.mapped
}
.map(\.original)
}
@WhiteHyun
WhiteHyun / 300. Longest Increasing Subsequence.swift
Created July 19, 2024 19:49
LeetCode - 300. Longest Increasing Subsequence using Greedy & Binary Search
final class Solution {
func lengthOfLIS(_ nums: [Int]) -> Int {
var tails: [Int] = [nums[0]]
for index in 1 ..< nums.count {
if nums[index] > tails.last! {
tails.append(nums[index])
} else {
tails[binarySearch(tails, target: nums[index])] = nums[index]
@WhiteHyun
WhiteHyun / 300. Longest Increasing Subsequence.swift
Created July 19, 2024 19:09
LeetCode - 300. Longest Increasing Subsequence using DP
final class Solution {
func lengthOfLIS(_ nums: [Int]) -> Int {
var dp: [Int] = .init(repeating: 1, count: nums.count)
for i in 1..<nums.count {
for j in 0..<i where dp[i] <= dp[j] && nums[j] < nums[i] {
dp[i] = dp[j] + 1
}
}
@WhiteHyun
WhiteHyun / main.swift
Created June 18, 2024 07:12
Vapor with APIProtocol
import Foundation
import Vapor
import OpenAPIRuntime
import OpenAPIVapor
struct GreetingService: APIProtocol {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
let name = input.query.name ?? "Stranger"
let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!")
return .ok(.init(body: .json(greeting)))
@WhiteHyun
WhiteHyun / main.swift
Created June 18, 2024 06:27
Vapor - basic execution
import Vapor
let app = Application()
try app.run()
@WhiteHyun
WhiteHyun / openapi.yaml
Last active June 18, 2024 06:12
OpenAPI yaml
openapi: "3.1.0"
info:
title: "App"
version: "1.0.0"
servers:
- url: "https://api.path.com/v1"
description: "Production Server"
- url: "https://localhost/v1"
description: "Internal staging server for testing"