Created
August 7, 2018 15:44
-
-
Save boraseoksoon/605dcf4dd0709a9eebeda8817dcd69cf to your computer and use it in GitHub Desktop.
20180808.Playground
This file contains hidden or 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
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| import PlaygroundSupport | |
| let testList = ["a", "b", "c", "d"] | |
| let tokenStr = testList.joined(separator: ", ") | |
| tokenStr | |
| let multiLanguageAddressList = | |
| [ | |
| "2089-40 목동, 강서구, 서울특별시, 대한민국, 🇰🇷", | |
| "3089-40 mok-dong, Gangseo-gu, Seoul-jikhalsi, Zuid-Korea, 🇰🇷", | |
| "4089-40 묵동, 강서구, Сеул, Южная Корея, 🇰🇷" | |
| ] | |
| typealias AddressWord = String | |
| func ParseAddressString(inputText: [String]) -> [AddressWord] { | |
| return inputText | |
| .joined(separator: ",") | |
| .components(separatedBy: "\n") | |
| .flatMap { $0.components(separatedBy: ",") } | |
| .flatMap { $0.components(separatedBy: " ") } | |
| .filter { $0 != "" } | |
| } | |
| public extension Sequence where Element: Equatable { | |
| var uniqueElements: [Element] { | |
| return self.reduce(into: []) { uniqueElements, element in | |
| if !uniqueElements.contains(element) { | |
| uniqueElements.append(element) | |
| } | |
| } | |
| } | |
| } | |
| let tokenList = ParseAddressString(inputText: multiLanguageAddressList).uniqueElements | |
| let myString = "1" | |
| CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: myString)) | |
| let testArr = ["str1", "str2"] | |
| //let testArr = [["str1", "str2"], ["test1", "test2"]] | |
| let testResult = testArr.joined(separator: ",") | |
| testResult | |
| extension String { | |
| func containsInValidCharacter(at: Int) -> Bool { | |
| let characterSet = CharacterSet(charactersIn: "[]{}%^*+=_'\'|~<>.,?!`-/:;()$&@") | |
| let range = (self as NSString).rangeOfCharacter(from: characterSet) | |
| return range.location == at | |
| } | |
| var containsInValidCharacter: Bool { | |
| let characterSet = CharacterSet(charactersIn: "[]{}%^*+=_'\'|~<>.,?!`-/:;()$&@") | |
| let range = (self as NSString).rangeOfCharacter(from: characterSet) | |
| return range.location != NSNotFound | |
| } | |
| } | |
| "abcd".containsInValidCharacter | |
| "!bcd".containsInValidCharacter | |
| "#bcd".containsInValidCharacter | |
| "a$bcd".containsInValidCharacter(at: 1) | |
| "!bcd".containsInValidCharacter(at: 0) | |
| "#bc*".containsInValidCharacter(at: 3) | |
| var nested = [["test", "test2"], ["test", "variables2"], ["good1", "good2"], ["very1"]] | |
| var targetStr = "longaddress" | |
| let flattend = nested.flatMap { $0 } | |
| let mapped = nested.map { $0 } | |
| flattend | |
| mapped | |
| var intStrList = ["0","1","2","3","4","5","6","7","8","9","10"] | |
| let result0 = intStrList.joined(separator: ",") | |
| let result = intStrList.reduce("") { $0 == "" ? $1 : $0 + "," + $1 } | |
| // let result = intList.reduce("", combine: { $0 == "" ? $1 : $0 + "," + $1 }) | |
| result0 | |
| result | |
| //let result = nested.reduce([], { $0.append($1) }) | |
| //result | |
| let map = nested.map { $0.joined(separator: ",") } | |
| print("map : ", map) | |
| let flat = nested.flatMap { $0.joined(separator: ",") } | |
| print("map : ", flat) | |
| //extension String { | |
| // public func separate(withChar char : String) -> [String]{ | |
| // var word : String = "" | |
| // var words : [String] = [String]() | |
| // for chararacter in self { | |
| // if String(chararacter) == char && word != "" { | |
| // words.append(word) | |
| // word = char | |
| // }else { | |
| // word += String(chararacter) | |
| // } | |
| // } | |
| // words.append(word) | |
| // return words | |
| // } | |
| //} | |
| // | |
| // let separate = "test abc".separate(withChar: " ") | |
| //let components = "test abc".components(separatedBy: " ") | |
| typealias DescriptionWord = String | |
| func ParseEventDescription(inputText: String) -> [DescriptionWord] { | |
| return inputText.components(separatedBy: "\n").flatMap { $0.components(separatedBy: " ") } | |
| } | |
| let testStr = """ | |
| test is important in software but it | |
| is often ignored that this is seriously necessary in programming. | |
| QA these days are really important role | |
| """ | |
| //let trimmedTest = testStr.trimmingCharacters(in: .whitespacesAndNewlines) | |
| //let result = ParseEventDescription(inputText: trimmedTest) | |
| //result | |
| let originHashTag = ["swift", "swift", "javascript"] | |
| let newHashTag = ["swift", "swift", "swift", "clojure"] | |
| //let originHashTag = [String]() | |
| //let newHashTag = [String]() | |
| // new, keep, remove | |
| typealias HashTag = String | |
| typealias NewHashTag = String | |
| typealias KeepHashTag = String | |
| typealias RemoveHashTag = String | |
| func DiffHashTags(compare newHashTags: [HashTag], to originHashTags:[HashTag]) -> ([NewHashTag], [KeepHashTag], [RemoveHashTag]) { | |
| var news = [String]() | |
| var keeps = [String]() | |
| var removes = [String]() | |
| news = newHashTag.filter { !originHashTags.contains($0) } | |
| removes = originHashTags.filter { !newHashTag.contains($0) } | |
| keeps = newHashTag.filter { !news.contains($0) } | |
| return (news, keeps, removes) | |
| } | |
| let tuple = DiffHashTags(compare: newHashTag, to: originHashTag) | |
| let news = tuple.0.uniqueElements | |
| let keeps = tuple.1.uniqueElements | |
| let removes = tuple.2.uniqueElements |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment