What would happen if I kept touching java7 all the time and suddenly wrote this process in another language? I thought, so I'll think about it for a moment. That's why I thought about the processing of arrays, which is common in web applications.
↓ Narrow the list of json or map to age> 25, then sort by age in ascending order
[
{"age":25, "name": "adam"},
{"age":60, "name": "adamo"},
{"age":30, "name": "larry"}
]
In python, higher-order functions are defined by built-in functions, so as you write, the nesting gradually becomes deeper. Because of that, it's hard to understand what kind of array processing is done for what even with such a source.
users= [
{"name":"adam","age":25},
{"name":"adamo","age":60},
{"name":"larry","age":30}
]
sorted(
filter(lambda a: a['age']>25,
users
),
key=lambda a:a['age']
)
Currying is performed using partial, and method chains are performed using reduce. It doesn't seem like it's going to come out with this alone, but it may be better if it's a guy who can postpone the evaluation by returning a method like xrange.
from functools import partial
def chain(*args):
return reduce(lambda x,f:f(x), args)
filter25 = partial(filter, lambda a: a['age']>25)
sortbyage = partial(sorted, key=lambda a:a['age'])
users=[
{"name":"adam","age":25},
{"name":"adamo","age":60},
{"name":"larry","age":30}
]
chain(
users,
filter25,
sortbyage
)
simple.
val users: List[Map[Symbol,String]] = List(
Map('age->"25", 'name->"adam"),
Map('age->"60", 'name->"adamo"),
Map('age->"30", 'name->"larry")
)
users.filter(n => n.get('age).get.toInt > 25)
.sortBy(_.get('age).get)
If you keep the map, it lacks safety. I think it's a good idea to replace it with an array of classes at the beginning or map it in the middle.
//Pattern to change from the beginning
val users: List[Person] = List(
new Person(25, "adam"),
new Person(60,"adamo"),
new Person(30,"larry")
)
users.filter(n => n.age > 25)
.sortBy(_.age)
//Pattern to change on the way
val users: List[Map[Symbol,String]] = List(
Map('age->"25", 'name->"adam"),
Map('age->"60", 'name->"adamo"),
Map('age->"30", 'name->"larry")
)
users.map(n => new Person(n('age).toInt, n('name)))
.filter(n => n.age > 25)
.sortBy(_.age)
In fact, there was a person who wrote such a source. It's a little easier to understand if you have knowledge of guava ... I wrote it properly, so when I noticed it, I used the class. The request is different from that of python.
List<Map<String, String>> users = ImmutableList.of<Map<String, String>>(
ImmutableMap.of("name", "adam", "age", "25"),
ImmutableMap.of("name", "adamo", "age", "60"),
ImmutableMap.of("name", "larry", "age", "30")
);
FluentIterable
.from(users)
.transform(new Function<Map<String, String>, Person>(){
@Override
public Person apply(Map<String,String> map){
return new Person(map.get("name"), new Integer(map.get("age")));
}
})
.filter(new Predicate<Person>(){
@Override
public boolean apply(Person person){
return person.age > 25;
}
})
.toSortedImmutableList(Ordering.natural().onResultOf(
new Function<Person, Integer>(){
@Override
public Integer apply(Person person){
return person.age;
}
})
);
It is undeniable that the sort part is a little old-fashioned. The definition of the first data is quite annoying. Which is the best way?
List users = Collections.unmodifiableList(Arrays.asList(
new HashMap<String, String>(){{put("age","25");put("name","adam");};},
new HashMap<String, String>(){{put("age","60");put("name","adamo");};},
new HashMap<String, String>(){{put("age","30");put("name","larry");};}
)
);
users.stream()
.filter(a -> new Integer(a.get("age")) > 25)
.sorted((o1, o2) -> new Integer(o1.get("age")).compareTo(new Integer(o2.get("age"))))
.collect(Collectors.toList());
When I stored this in an object on the way, it became quite straightforward.
List users= Collections.unmodifiableList(Arrays.asList(
new HashMap<String, String>(){{put("age","25");put("name","adam");};},
new HashMap<String, String>(){{put("age","60");put("name","adamo");};},
new HashMap<String, String>(){{put("age","30");put("name","larry");};}
)
)
users.stream()
.map(a -> new Person(new Integer(a.get("age")), a.get("name")))
.filter(a -> a.age > 25)
.sorted(comparing(Person::getAge))
.collect(Collectors.toList());
What happened because I saw only such a narrow thing? It feels like that, but at least I thought it would be better to throw away java7 and upgrade to java8.
I haven't written it this time, but since it's java7, if I proceed normally with procedural programming, I feel like I can't say anything because the code with side effects is written like "I'll write here as well" even with this kind of array processing, so the stream of java8 It is very attractive to be able to create code conventions with API.
Recommended Posts