Posts

Showing posts with the label lambda

Applying lambda on Python DataFrame

Applying lambda on Python DataFrame I am having a Python Pandas DataFrame like >>> df classification like 0 flower 1 1 flower 0 2 flower 0 3 adventure 1 4 adventure 1 I want to create an output DataFrame like >>> df classification like liked 0 flower 1 True 1 flower 0 False 2 flower 0 False 3 adventure 1 True 4 adventure 1 True I am "apply"ing the Python lambda function on the input DataFrame as follows: >>> df['like'].apply(lambda x: x == 1) But I am getting all 'False' under the 'liked' column >>> df classification like liked 0 flower 1 False 1 flower 0 False 2 flower 0 False 3 adventure 1 False 4 adventure 1 False Any quick suggestions will be helpful. >>> df['like'].astype(int) 0 1 1 0 2 0 3 1 4 1 Name: like, dtype: int...

Collect the result with only a stream (without external loop use)

Collect the result with only a stream (without external loop use) Is there a way to do the following code only with lambdas ? // translate someList1 to someList3 // .. get sublist List<String> someList2 = someList1.stream() .map(i -> i.getField()) .collect(Collectors.toList()); // .. create new (target) list List<SomeClass> someList3 = new ArrayList<>(); for (String item : someList2) { SomeClass someObj = new SomeClass(); someObj.setField(item); someList3.add(someObj); } 3 Answers 3 Just one collect is required : List<SomeClass> someList2 = someList1.stream() .map(i -> { SomeClass someObj = new SomeClass(); someObj.setField(i.getField()); return someObj; } ) .collect(Collectors.toList()); But note that with a constructor...

private MyDbContext _appContext => (MyDBContext)_context;?

private MyDbContext _appContext => (MyDBContext)_context;? Please can some explain what does this code means ? private MyDbContext _appContext => (MyDBContext)_context; Thanks alot you declare a private property _appContext which returns _context as MyDBContext – Pierre 4 mins ago _appContext _context as MyDBContext By calling _appContext.Anything() you actually call ((MyDBContext)_context).Anything() – Pierre 2 mins ago _appContext.Anything() ((MyDBContext)_context).Anything() 1 Answer 1 Basically its an Expression-bodied member introduced in C# 6 Expression body definitions l...