Blog 11: reduce | (Open) | (Share)
▲ Previous | Next ▼/Dev
(Last modified: December 17 2024 02:16:56)reduce
Here is the extracted code from all the images:
1. Grouping Topics and Utterances
Topics Array
const topics = [
{ topic: 'Topic 1', start: 0 },
{ topic: 'Topic 2', start: 12 },
{ topic: 'Topic 3', start: 24 },
];
Utterances Array
const utterances = [
{ text: 'Hello Welcome to the show', start: 0, end: 3 },
{ text: 'JavaScript popular for web dev', start: 4, end: 7 },
{ text: 'Variables declared with var, let', start: 8, end: 11 },
{ text: 'Prototype inheritance in JavaScript', start: 12, end: 15 },
{ text: 'DOM API manipulates HTML', start: 16, end: 19 },
{ text: 'Callbacks used for async code', start: 20, end: 23 },
{ text: 'Runs client-side and server-side', start: 24, end: 27 },
{ text: 'React and Vue popular frameworks', start: 28, end: 31 },
{ text: 'Node.js runs JavaScript outside browser', start: 32, end: 35 }
];
Group Utterances into Topics
const groupedTopics = Map.groupBy(utterances, (utterance) =>
topics.find((topic, i) => {
const { start } = topic;
const end = topics.at(i + 1)?.start ?? Infinity;
return utterance.start >= start && utterance.end <= end;
})
);
console.log(groupedTopics);
2. Grouping Orders
Orders Array
const orders = [
{ countryCode: 'US', name: 'John', product: 'stickers' },
{ countryCode: 'CA', name: 'Sarah', product: 'shirt' },
{ countryCode: 'MX', name: 'Carlos', product: 'skateboard' },
{ countryCode: 'FR', name: 'Marie', product: 'hoodie' },
{ countryCode: 'JP', name: 'Yuki', product: 'basketball' },
{ countryCode: 'US', name: 'Emily', product: 'stickers' },
{ countryCode: 'US', name: 'Scott', product: 'shirt' },
{ countryCode: 'CA', name: 'David', product: 'shirt' },
{ countryCode: 'MX', name: 'Juan', product: 'skateboard' }
];
Group Orders by Product
const { shirt, stickers } = Object.groupBy(orders, (order) => order.product);
console.log(shirt);
Group Orders by Country Code
const ordersByCountry = Object.groupBy(orders, (order) => order.countryCode);
console.log(ordersByCountry);
Using reduce to Group Orders
const ordersByCountryR = orders.reduce((acc, order) => {
const { countryCode } = order;
if (!acc[countryCode]) {
acc[countryCode] = [];
}
acc[countryCode].push(order);
return acc;
}, {});
console.log(ordersByCountryR);
Output Examples
-
Grouped Topics Example:
Map(3) { "Topic 1" => [ { text: "Hello Welcome to the show", start: 0, end: 3 }, { text: "JavaScript popular for web dev", start: 4, end: 7 }, { text: "Variables declared with var, let", start: 8, end: 11 } ], "Topic 2" => [ { text: "Prototype inheritance in JavaScript", start: 12, end: 15 }, { text: "DOM API manipulates HTML", start: 16, end: 19 }, { text: "Callbacks used for async code", start: 20, end: 23 } ], "Topic 3" => [ { text: "Runs client-side and server-side", start: 24, end: 27 }, { text: "React and Vue popular frameworks", start: 28, end: 31 }, { text: "Node.js runs JavaScript outside browser", start: 32, end: 35 } ] } -
Grouped Orders by Country Code:
{ "US": [ { countryCode: "US", name: "John", product: "stickers" }, { countryCode: "US", name: "Emily", product: "stickers" }, { countryCode: "US", name: "Scott", product: "shirt" } ], "CA": [ { countryCode: "CA", name: "Sarah", product: "shirt" }, { countryCode: "CA", name: "David", product: "shirt" } ], "MX": [ { countryCode: "MX", name: "Carlos", product: "skateboard" }, { countryCode: "MX", name: "Juan", product: "skateboard" } ], "FR": [{ countryCode: "FR", name: "Marie", product: "hoodie" }], "JP": [{ countryCode: "JP", name: "Yuki", product: "basketball" }] }
Notes:
- The
Object.groupBymethod is a modern way to group objects by a key. It works efficiently and cleanly. - The
reducemethod can achieve the same functionality but requires more manual setup.
Blog 12: The best way to build an app for Android and iOS depends on your goals, team expertise, budget, and | (Open) | (Share)
▲ Previous/Dev/Tutorials
(Last modified: November 20 2024 21:35:09)The best way to build an app for Android and iOS depends on your goals, team expertise, budget, and desired features. Here are the primary approaches:
1. Native Development
- Languages:
- Android: Kotlin or Java
- iOS: Swift or Objective-C
- Tools:
- Android Studio for Android
- Xcode for iOS
- Advantages:
- Best performance and access to platform-specific features.
- Highly optimized user experience (UI/UX).
- Greater control over hardware features.
- Disadvantages:
- Requires separate codebases for Android and iOS.
- Higher development cost and effort.
2. Cross-Platform Development
Build apps using a single codebase that works for both platforms.
a. Flutter
- Language: Dart
- Advantages:
- High performance due to native compilation.
- Rich pre-designed widgets for a consistent UI.
- Fast development with hot reload.
- Growing community support.
- Disadvantages:
- App size can be slightly larger.
- Platform-specific feature integration can be complex.
b. React Native
- Language: JavaScript/TypeScript
- Advantages:
- Reusable codebase for Android, iOS, and web.
- Strong community and library ecosystem.
- Good performance for most use cases.
- Disadvantages:
- Native modules may be needed for complex features.
- Performance slightly lags behind Flutter and native.
c. Xamarin (by Microsoft)
- Language: C#
- Advantages:
- Tight integration with Microsoft tools.
- Shared codebase for logic across platforms.
- Suitable for enterprise apps.
- Disadvantages:
- Smaller community.
- Apps can be bulky.
d. Other Options
- Ionic (HTML, CSS, JavaScript; uses web views).
- NativeScript (JavaScript, TypeScript; deep native integration).
3. Progressive Web Apps (PWAs)
- What It Is:
- A web app that functions like a native app on mobile.
- Advantages:
- Single codebase for all platforms.
- No need to publish in app stores.
- Cost-effective.
- Disadvantages:
- Limited access to device features.
- Cannot match the performance of native apps.
4. No-Code/Low-Code Platforms
- Tools: Adalo, Bubble, Glide
- Advantages:
- Minimal technical skills required.
- Fast development for simple apps.
- Disadvantages:
- Limited scalability and customization.
- Performance issues for complex apps.
Recommendations Based on Use Case
- Performance-Critical Apps (e.g., gaming, AR/VR): Go native.
- Feature-Rich Apps with Budget Constraints: Use Flutter or React Native.
- Simple Apps or MVPs: PWAs or No-Code platforms.
- Enterprise Apps with Microsoft Ecosystem: Xamarin.
Would you like help choosing a framework or setting up the development environment?