Arrays & Functional Pipelines Across JavaScript, C#, and Java
๐ง 1. Core Philosophy
| Language | Nature of Arrays |
| JavaScript | Dynamic, flexible, loosely typed |
| C# | Fixed-size, strongly typed |
| Java | Fixed-size, strongly typed |
Real-world usage:
- JavaScript → flexible pipelines
- C#/Java → prefer collections (List, ArrayList)
---
๐ฆ 2. Collections Overview
C# Collections
Non-Generic (Legacy)
| Class | Generic Version? |
| ArrayList | No |
| Hashtable | No |
| Stack | No |
| Queue | No |
Generic (Modern)
| Class |
| List<T> |
| Dictionary<K,V> |
| HashSet<T> |
| Queue<T> |
| Stack<T> |
| LinkedList<T> |
| SortedList<K,V> |
| SortedDictionary<K,V> |
---
Java Collections
| Category | Examples |
| List | ArrayList, LinkedList |
| Set | HashSet, TreeSet |
| Map | HashMap, TreeMap |
| Queue | PriorityQueue, Deque |
---
JavaScript Collections
- Array
- Map
- Set
- WeakMap
- WeakSet
---
๐ 3. Functional Pipeline Mapping
| Concept | JavaScript | C# (LINQ) | Java (Streams) |
| Transform | map | Select | map |
| Filter | filter | Where | filter |
| Reduce | reduce | Aggregate | reduce |
| FlatMap | flatMap | SelectMany | flatMap |
| Find | find | First | findFirst |
| Any | some | Any | anyMatch |
| All | every | All | allMatch |
| Contains | includes | Contains | contains |
| Sort | sort | OrderBy | sorted |
---
⚙️ 4. JavaScript Array Methods
Mutating
- push, pop
- shift, unshift
- splice
- sort, reverse
Non-Mutating
- map
- filter
- slice
- concat
- flat, flatMap
Value Returning
- reduce
- find
- includes
- some, every
---
๐ค 5. Return Types
| Method Type | Returns |
| map/filter/flat | New array |
| reduce | Single value |
| find | Element |
| some/every | Boolean |
| push/unshift | New length |
| pop/shift | Removed element |
| splice | Removed elements (array) |
| sort/reverse | Mutated array |
---
๐ฅ 6. map vs reduce
map
[1,2,3].map(x => x * 2) → [2,4,6]
reduce
[1,2,3].reduce((acc,x) => acc+x,0) → 6
Rule: map = transform, reduce = combine
---
๐งฎ 7. reduce Explained
With initial value
arr.reduce((acc,x)=>acc+x,0)
Without initial value
arr.reduce((a,b)=>a+b)
Best practice: Always provide initial value
---
๐ 8. flat vs flatMap
flat
[1,[2,3]].flat() → [1,2,3]
flatMap
[1,2,3].flatMap(x => [x,x*2]) → [1,2,2,4,3,6]
---
๐ 9. Equivalent in C# and Java
C#
arr.Select(...)
arr.Where(...)
arr.Aggregate(...)
arr.SelectMany(...)
Java
stream.map(...)
stream.filter(...)
stream.reduce(...)
stream.flatMap(...)
---
๐ 10. Iterator vs Enumerator
| Concept | Java | C# |
| Pattern | Iterator | Enumerator |
| Move | hasNext() | MoveNext() |
| Value | next() | Current |
---
⚡ 11. Performance
for loop → fastest
foreach → cleaner
stream → readable but overhead
---
๐ 12. Functional vs Streaming
- Functional: map, filter, reduce
- Streaming: file/network streams
---
๐ก 13. Kafka & Arrays
C# Object → JSON/Avro → Kafka → Java Object
---
๐งพ 14. Avro
- Binary format
- Schema-based
- Efficient
---
๐ง Final Mental Model
Arrays → Data
map → Transform
filter → Select
flatMap→ Expand
reduce → Combine
---
๐ Key Takeaways
- Arrays are data pipelines
- flatMap / SelectMany is critical
- reduce is powerful but tricky
- Prefer functional style
- Kafka uses serialized data
No comments:
Post a Comment