Deep_Dev
article thumbnail

 

๋งต, ํ•„ํ„ฐ, ๋ฆฌ๋“€์Šค

์Šค์œ„ํ”„ํŠธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ผ๊ธ‰ ๊ฐ์ฒด๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜๋ฅผ ๊ณ ์ฐจํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ๋Œ€ํ‘œ์ ์ธ ๊ณ ์ฐจํ•จ์ˆ˜๋กœ ๋งต, ํ•„ํ„ฐ, ๋ฆฌ๋“€์Šค๊ฐ€ ์žˆ๋‹ค.

 

 ๋งต

 ์ž์‹ ์„ ํ˜ธ์ถœํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

  • ์Šค์œ„ํ”„ํŠธ์—์„œ ๋งต์€ ๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ์„ธํŠธ, ์˜ต์…”๋„ ๋“ฑ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

for-in ๊ตฌ๋ฌธ๊ณผ ๋งต ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ ๋น„๊ต

let numbers: [Int] = [0,1,2,3,4]

var doubledNumbers: [Int] = [Int]()
var strings: [String] = [String]()

// for ๊ตฌ๋ฌธ ์‚ฌ์šฉ
for number in numbers {
	doubledNumbers.append(number*2)
    strings.append("\(number)")
}

print(doubledNumbers) // [0,2,4,6,8]
print(strings) // ["0","1","2","3","4"]

// map ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
doubledNumbers = number.map({ (number: Int) -> Int in
	return number*2
})
strings = number.map({ (number: Int) -> String in
	return "\(number)"
})

print(doubledNumbers) // [0,2,4,6,8]
print(strings) // ["0","1","2","3","4"]

์งง์€ ์ฝ”๋“œ๋ผ ์ฐจ์ด๋ฅผ ๋Š๋ผ๊ธฐ๋Š” ํž˜๋“ค๊ฒ ์ง€๋งŒ, map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ for-in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ฐ ์š”์†Œ์˜ ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‹ฌ์ง€์–ด map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด for-in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋นˆ ๋ฐฐ์—ด์„ ์ฒ˜์Œ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์ž‘์—…๋„ ํ•„์š”์—†๋‹ค.

 

ํ•„ํ„ฐ

์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ฑธ๋Ÿฌ์„œ ์ถ”์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ณ ์ฐจํ•จ์ˆ˜

  • ๋งต๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ์— ๊ฐ’์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.๋‹ค๋งŒ ๋งต์ฒ˜๋Ÿผ ๊ธฐ์กด ์ปจํ…์ธ ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŠน์ • ์กฐ๊ฑด์— ๋งž๊ฒŒ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ์—ญํ• 
  • filter ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์€ Bool ์ด๋‹ค. ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ์— ํฌํ•จ๋  ํ•ญ๋ชฉ์ด๋ผ๊ณ  ํŒ๋‹จํ•˜๋ฉด true๋ฅผ, ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

ํ•„ํ„ฐ ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ

let numbers: [Int] = [0,1,2,3,4,5]

let evenNumbers: [Int] = numbers.filter { (number: Int) -> Bool in
	return number % 2 == 0
}
print(evenNumbers) // [0,2,4]

let oddNumbers: [Int] = numbers.filter{ $0 % 2 == 1 }
print(oddNumbers) // [1,3,5]

๋งŒ์•ฝ ์ฝ˜ํ…์ธ ์˜ ๋ณ€ํ˜• ํ›„์— ํ•„ํ„ฐ๋งํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋งต์„ ์‚ฌ์šฉํ•œ ํ›„์— ํ•„ํ„ฐ ๋ฉ”์Šค๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

 

๋ฆฌ๋“€์Šค

์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ์ฝ˜ํ…์ธ ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉํ•˜๋Š” ๊ธฐ๋Šฅ ( ๊ฒฐํ•ฉ )

 

๋ฆฌ๋“€์Šค์˜ 2๊ฐ€์ง€ ํ˜•ํƒœ

1. ํด๋กœ์ €๊ฐ€ ๊ฐ ์š”์†Œ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์—ฐ์‚ฐํ•œ ํ›„ ๊ฐ’์„ ๋‹ค์Œ ํด๋กœ์ € ์‹คํ–‰์„ ์œ„ํ•ด ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆœํ™˜ํ•˜๋Š” ํ˜•ํƒœ

public func reduce<Result>(_ initialResult: Result,
	_ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result

initialResult๋ผ๋Š” ์ด๋ฆ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์„ ํ†ตํ•ด ์ดˆ๊นƒ๊ฐ’์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, nextPartialResult๋ผ๋Š” ์ด๋ฆ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํด๋กœ์ €๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค. nextPartialResult ํด๋กœ์ €์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฆฌ๋“€์Šค ๋ฉ”์„œ๋“œ์˜ initialResult๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฐ›์€ ์ดˆ๊นƒ๊ฐ’ ๋˜๋Š” ์ด์ „ ํด๋กœ์ €์˜ ๊ฒฐ๊ด๊ฐ’์ด๋‹ค. ๋ชจ๋“  ์ˆœํšŒ๊ฐ€ ๋๋‚˜๋ฉด ๋ฆฌ๋“€์Šค์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์ด ๋œ๋‹ค. ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฆฌ๋“€์Šค ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆœํ™˜ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ์š”์†Œ์ด๋‹ค.

 

2. ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆœํ™˜ํ•˜๋ฉฐ ํด๋กœ์ €๊ฐ€ ์‹คํ–‰๋˜์ง€๋งŒ ํด๋กœ์ €๊ฐ€ ๋”ฐ๋กœ ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ํ˜•ํƒœ

๋Œ€์‹  inout ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ดˆ๊นƒ๊ฐ’์— ์ง์ ‘ ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

public func reduce<Reuslt>(into initialResult: Result,
	_ updateAccumlatingResult: (inout Result, Element) throws -> ()) rethrows ->
   	Result

updateAccumulatingResult๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ํด๋กœ์ €์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ inout๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค. updateAccumulatingResult ํด๋กœ์ €์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฆฌ๋“€์Šค ๋ฉ”์„œ๋“œ์˜ initialResult ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ „๋‹ฌ๋ฐ›๋Š” ์ดˆ๊นƒ๊ฐ’ ๋˜๋Š” ์ด์ „์— ์‹คํ–‰๋œ ํด๋กœ์ € ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ๋˜์–ด ์žˆ๋Š” ๊ฒฐ๊ด๊ฐ’์ด๋‹ค. ๋ชจ๋“  ์ˆœํšŒ๊ฐ€ ๋๋‚˜๋ฉด ๋ฆฌ๋“€์Šค์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์ด ๋œ๋‹ค. ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฆฌ๋“€์Šค ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆœํ™˜ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ์š”์†Œ์ด๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋ฆฌ๋“€์Šค๋ฅผ ๋งต๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.