Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Array Grouping will soon be arriving at TypeScript
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
jiko21
February 21, 2024
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Array Grouping will soon be arriving at TypeScript
kansaits#5の資料です
jiko21
February 21, 2024
More Decks by jiko21
See All by jiko21
型情報を手繰り寄せる技術~TypeScript Compiler APIによる型解析実践~
jiko21
0
1.2k
Creating a Next.js-style Framework with Bun and Hono
jiko21
0
190
Copying Array Methods arrived at TypeScript
jiko21
1
980
SSRで動的に OGP画像を生成したい! 〜Cloudflare Workersから@vercel/og移行編〜
jiko21
0
170
node:test will replace Jest?
jiko21
0
120
どこでも動かすために… TypeScriptでライブラリ開発の すゝめ
jiko21
2
470
NestJS a progressive web framework
jiko21
3
2.3k
レガシーなフロントエンドをリプレイスする
jiko21
5
1.6k
Deep Dive Into Vue Composition API
jiko21
0
3.3k
Featured
See All Featured
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Technical Leadership for Architectural Decision Making
baasie
3
580
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
200
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
260
Google's AI Overviews - The New Search
badams
0
1.6k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Building an army of robots
kneath
307
46k
Done Done
chrislema
186
17k
Transcript
SELECT keyName, valueArray(keyName, array) FROM array GROUP BY keyName Array
Grouping will soon be KansaiTS#5 @jiko21 arriving at TypeScript
About jiko21… Name: Daiki Kojima (jiko21) Multistack Engineer @ AppBrew.inc
Love: Guitar, TypeScript, baseball @jiko21 @jiko_21
Array grouping
About array grouping method • ڈʹͳͬͯECMAScriptʹೖͬͨproposal • Α͏͘TypeScript 5.4Ͱܕใ͕Ճ͞ΕΔʂ •
ରԠ͞ΕͯΔϒϥβ͔ͳΓݶఆత https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
About array grouping method • ྻཁૉΛgroupԽͨ͠object(Object.groupBy) map(Map.groupBy)͕ੜͰ͖Δ • SQLͰ͍͏GroupByͬΆ͍ (sumͱ͔count͕Ͱͯ͘ΔΘ͚Ͱͳ͍͕)
const array = [1, 2, 3, 4, 5]; const groupByResult = Object.groupBy(array, (nu m ) => { return num % 2 === 0 ? 'even': 'odd'; }); /** * { * 'even': [2, 4], * 'odd': [1, 3, 5], * } */ console.log(groupByResult)
ࠓ·Ͱͩͱ… const array = [1, 2, 3, 4, 5]; const
result = { even: [] as number[], odd: [] as number[], }; array.forEach((value) => { if (value % 2 === 0 ) { result.even.push(value) } else { result.odd.push(value) } }); GPS&BDIͰճͯͦ͠ΕͧΕཁૉΛQVTI
ࠓ·Ͱͩͱ… const array = [1, 2, 3, 4, 5]; const
result = array.reduce((prevValue, currentValue) => { if (currentValue % 2 === 0 ) { prevValue.even.push(currentValue) } else { prevValue.odd.push(currentValue) } return prevValue; }, { even: [] as number[], odd: [] as number[], }); "SSBZSFEVDFͰແཧΓ͓͠ΌΕʹ
ͳΜͰstatic methodͳͷ͔ʁ • ॳArray.prototype.groupByΛߟ͍͕͑ͯͨ͢Ͱʹsugar ͱ͍͏ϥΠϒϥϦ͕groupByΛmonkey patchͰͯͯͨ • Array.prototype.groupߟ͕͑ͨྻΛHashMapʹ͢Δίʔυɹ ʹͿ͔ͭΔͷͰ݁ہstatic methodʹ
• https://github.com/tc39/proposal-array-grouping/ ʹৄࡉ͕ࡌ ͬͯ·͢
͍ॴ
͜͏͍͏σʔλΛද͍ࣔͨ͠… const todos = [ { status: 'TODO', text: 'إΛચ͏',
}, { status: 'TODO', text: 'ࣃΛຏ͘', }, { status: 'DOING', text: '෩࿊ʹೖΔ', }, { status: 'DONE', text: 'ञΛҿΉ', }, ];
݁ߏ؆୯ʹॻ͚·͢! function App() { const groupedTodos: { 'TODO': Todo[]; 'DOING':
Todo[]; 'DONE': Todo[]; } = Object.groupBy(todos, (ite m ) => { return ite m .status; }); return ( <div> <List status={'todo'} list={groupedTodos. T ODO. m ap((ite m ) => ite m .text)} /> <List status={'doing'} list={groupedTodos.DOING. m ap((ite m ) => ite m .text)} /> <List status={'done'} list={groupedTodos.DONE. m ap((ite m ) => ite m .text)} /> </div> ) } 'JMUFSࠈ͔Βൈ͚ग़ͤΔʂ
ύϑΥʔϚϯε
ύϑΥʔϚϯε • ৽͍͠method͕ੜ͑ͯɺܕใՃ͞Εͨͱ͍͏͜ͱɺɹɹɹ ͦΕ૬Ԡʹ͍͍…ͣʂ • ύϑΥʔϚϯεʹ͕͋Δͱ৽͍͠ϝιου͕͋ͬͯ͏ͷ͕ɹ ͨΊΒΘΕΔ…
࣮ݧͯ͠ΈΔ • 0, 1, …, N-1͕ೖͬͨྻ͔ΒҎԼͷΑ͏ͳobjectΛੜ͢Δ • ࣮ߦڥ • bun:
v1.0.25 • ϚγϯεϖοΫ: Apple M1 Pro / 32GB { even: [ 0 , 2, ...], odd: [1, 3, ...] }
Object.groupBy͜ΕͰൺֱ inputArray.reduce((prevValue, currentValue) => { if (currentValue % 2 ===
0 ) { prevValue.even.push(currentValue) } else { prevValue.odd.push(currentValue) } return prevValue; }, { even: [] as number[], odd: [] as number[], }) Object.groupBy(inputArray, (nu m ) => { return num % 2 === 0 ? "even": "odd"; }) WT
ύϑΥʔϚϯε ͳΜ͔શൠతʹॏ͍ʜ
Map.groupBy͜ΕͰൺֱ const map = new Map(); const evenArray: number[] =
[]; const oddArray: number[] = []; inputArray.forEach((currentValue) => { if (currentValue % 2 === 0 ) { evenArray.push(currentValue); } else { oddArray.push(currentValue); } }); map.set('even', evenArray); map.set('odd', oddArray); Map.groupBy(inputArray, (nu m ) => { return num % 2 === 0 ? "even": "odd"; }) WT
ύϑΥʔϚϯε ͋Μ·Γ͕ࠩͳ͍
͋ΕΕɺɺɺ
݁Ռඍົ • Map.groupByͦΜͳʹେࠩͳ͠… • Object.groupBy͚ͩ࿐ࠎʹ͘ͳͬͯͦ͏…
TC39 Array GroupingΛಡΜͰΈΔ… https://tc39.es/proposal-array-grouping/
ٖࣅίʔυΛॻ͍ͯΈΔͱ… function Object.groupBy(array, callback) { groups = GroupBy(array, callback, property);
obj = {}; for (g of groups<Key, Elements>) { elements = g.elements obj[g.key] = elements; } return obj } function Map.groupBy(array, callback) { groups = GroupBy(array, callback, property); map = new Map(); for (g of groups<Key, Elements>) { elements = g.elements map.set(g.key, elements); } return map; } GroupByΛڞ௨ͯ͠ݺΜͰΔ…
GroupByΛ͍ͯΈΔͱ… function GroupBy<T>(arr: T [], callback): {key: string; elements: T
[] }[] { groups = []; for(k = 0; k < arr.length(); k++) { /* ࡉ͔͍ॲཧ */ key = callback(arr[k]); AddValueToKeyedGroup(groups, key, arr[k]). } return groups } AddValueToKeyedGroupΛݺΜͰΔ… 0 / ճݺͼग़͞ΕΔ
AddValueToKeyedGroupΛ͍ͯΈΔͱ… function AddValueToKeyedGroup<T>(groups: {key: string; elements: T [] }[], key,
value): { for(group: groups) { if (group.key === key) { group.elements.push(value); return } } group = { key: key, elements: [value] } groups.push(group) } LFZͷݸ . ճ LFZ͕ͳ͚Ε৽ͨʹHSPVQTʹՃ
ͭ·Γ͜͏ͳΔ… "EE7BMVF5P,FZFE(SPVQ 0 . 0CKFDUHSPVQ#ZPS.BQHSPVQ#Z (SPVQ#Z 0 / 0 .
0 / /. ˠ0 /. Ͱ͋Δ ͜Ε·Ͱͩͱ.ͳͷͰ0 / ͰΑͦ͞͏ ͨͩ͠ϧʔϓ͕༨ܭʹଟΊʹճͬͯͦ͏
ͳΜͰ͜Μͳ͜ͱͯ͠Δͷ͔…(ߟ) • ObjectͱMapͰಉ༷͡(ͱ͍͏͔ڍಈ)ʹ͍ͨ͠ • key͕࣮࣌ʹະఆͰରԠͰ͖ΔΑ͏ʹ • ॻ͔ΕͯΔίʔυ2ྨͰࣗ໌ͳͷͰྑ͍ྫͱݴ͍ʹ͍͘ • ͜ͷΑ͏ʹ࣮͓͚ͯ͠৽͍͠key͕iteration࣌ʹݟ͔ͭͬͯɹ ରԠͰ͖Δ
ͪΐͬͱෆརͳϕϯν͔ͩͬͨͳ…
reduceଆΛվྑͯ͠ΈΔ inputArray.reduce((prevValue, currentValue) => { const key = currentValue %
2 === 0 ? 'even' : 'odd' if (prevValue.hasOwnProperty(key)) { prevValue[key].push(currentValue); } else { prevValue[key] = [currentValue]; } return prevValue; }, {}) LFZ͕࣮࣌ʹະఆͰ͍͍Α͏ʹ 0CKFDUHSPVQ#Zͷڍಈʹ߹ΘͤͯΔ
ύϑΥʔϚϯε ·͊·͊ಉ͘͡Β͍ ༗ҙͳࠩͳͦ͞͏
·ͱΊ
Array grouping Method·͍͍͊ͧʂ • groupingܥָ͕ʹ͔͚Δ • ͦΕҎ֎Ͱ fi lterࠈ͔Βൈ͚ग़͢ʹ͑ͦ͏ •
ύϑΥʔϚϯεҎԼʹҙ • O(NM)Ͱ͋Δ͕ϧʔϓճ͕ωοΫ? • keyͷछྨ͕࣮ߦ࣌ʹ͔Δ߹Ͱ͍͍Α͏ʹ… • ↑छྨ͕ݻఆͩͱͪΐͬͱ͋Εͳؾ͢Δ…
ࢀߟ • https://tc39.es/proposal-array-grouping/ • https://devblogs.microsoft.com/typescript/announcing- typescript-5-4-beta/#objectgroupby-and-mapgroupby • https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/ Global_Objects/Object/groupBy •
https://github.com/tc39/proposal-array-grouping/