public class Filter {
public static List<Number> apply(List<Number> lst, Double target){
return lst.stream()
.mapToDouble( Number::doubleValue )
.filter( elem -> elem > target )
.boxed()
.collect( Collectors.toCollection(ArrayList::new ));
}
public static void main(String[] args) {
// Integer[] nums = new Integer[] {1,2,3,4,5,6,7,8,9};
Double[] nums = new Double[] {2.1,3.2,4.3,5.4,6.5,7.6,8.7};
System.out.println(Filter.apply( Arrays.asList(nums), 5.0 )
);
}
}
CodePudding user response:
Remember that split/3 and merge/3 are ''help-predicates'' :
split([X], [], [X]).
split([H1, H2 | List], [H1 | ListA], [H2 | Result]) :-
split(List, ListA, Result).
merge([], L, L).
merge(L, [], L).
merge([H1 | List], [H2 | ListA], [H1 | Result]) :-
H1 < H2,
!,
merge(List, [H2 | ListA], Result).
merge(List, [H2 | ListA], [H2 | Result]) :-
merge(List, ListA, Result).
merge_sort([], []).
merge_sort([A], [A]).
merge_sort(List, SList):-
split(List, List1, List2),
merge_sort(List1, SList1),
merge_sort(List2, SList2),
merge(SList1, SList2, SList).
If you query, you should get this:
?- split([2, 5, 7, 4, 3, 1, 9, 8, 6], ListA, ListB).
ListA = [2, 7, 3, 9],
ListB = [5, 4, 1, 8, 6] .
?- merge([1, 3, 5, 7, 9], [2, 4, 6, 8], MergedList).
MergedList = [1, 2, 3, 4, 5, 6, 7, 8, 9] .
?- merge_sort([2, 5, 7, 4, 3, 1, 9, 8, 6], Result).
Result= [1, 2, 3, 4, 5, 6, 7, 8, 9].
CodePudding user response:
Something like this would work:
%---------------------------
% find all Xs greater than Y
%---------------------------
items_greater_than( [] , _ , [] ) .
items_greater_than( [X|Xs] , Y , [X|Zs ) :- X @> Y, items_greater_than(Xs,Y,Zs).
items_greater_than( [X|Xs] , Y , Zs ) :- X @=< Y, items_greater_than(Xs,Y,Zs).
Executing
items_greater_than( [2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7], 5.0, R ).
should yield
R = [ 5.4, 6.5, 7.6, 8.7 ]