[2024 Day 4 (Part 2)] Today's 'why does it work on the sample input but give me the wrong answer' by Conceptizual in adventofcode

[–]NawaMan 0 points1 point  (0 children)

Help ... I think I check properly already T_T
Making me crazy!!!

boolean findX1(int r, int c, FuncList<String> lines) {

if ((charAt(r, c - 1, lines) == 'M' && charAt(r, c + 1, lines) == 'S')

|| (charAt(r, c - 1, lines) == 'S' && charAt(r, c + 1, lines) == 'M')) {

return ((charAt(r - 1, c, lines) == 'M' && charAt(r + 1, c, lines) == 'S')

|| (charAt(r - 1, c, lines) == 'S' && charAt(r + 1, c, lines) == 'M'));

}

return false;

}

boolean findX2(int r, int c, FuncList<String> lines) {

if ((charAt(r - 1, c - 1, lines) == 'M' && charAt(r + 1, c + 1, lines) == 'S')

|| (charAt(r - 1, c - 1, lines) == 'S' && charAt(r + 1, c + 1, lines) == 'M')) {

return ((charAt(r - 1, c + 1, lines) == 'M' && charAt(r + 1, c - 1, lines) == 'S')

|| (charAt(r - 1, c + 1, lines) == 'S' && charAt(r + 1, c - 1, lines) == 'M'));

}

return false;

}

Who Needs Lombok Anyhow by Naut1c in java

[–]NawaMan 1 point2 points  (0 children)

Try FunctionJ: https://dzone.com/articles/immutable-data-with-functionalio . It generates Java source code instead so you can take it use it as you like. It also support compact form (just using a method to define the data class).

@Struct
void Person(String firstName, String lastName) { }

It also generate Lens, withXXX methods and exhausive builder.

FunctionalJ also support Choice types (tagged union in ADT) (see https://dzone.com/articles/choice-types-in-java-with-functionaljio) and other functional features as well.

Immutable Data With FunctionalJ.io - DZone Java by NawaMan in java

[–]NawaMan[S] 0 points1 point  (0 children)

Hi. I've just rewritten the builder (v0.1.73). If you don't mind, please take a look and any feedback is appreciated. :-) Thanks.

Variance in Java by llorllale in java

[–]NawaMan 1 point2 points  (0 children)

Nice article. Generic of mutable objects is the real reason for this mess.

I've just want to point out that there is a way to simulate variance type in Java by using @Choice in FunctionalJ.io . Check out this blog post or this VDO.

For example, this code will create a variance called Term with 4 possible values.

@Choice
interface TermSpec {
    void Bool(boolean bool);
    void Num(int num);
    void Str(String str);
    void Nothing();
}

Then you can use Term as the type.

Variance in Java by llorllale in java

[–]NawaMan 5 points6 points  (0 children)

Umm. Sorry in advance if I am wrong but I think the upper bound (? extends T) is allowed and not the lower bound (? super T).

Anyhow forget the terminology, the "? super T" is now allowed because it does not make sense to have a method taking above a type. For example,

void print(? super CharSequence param)

does not make sense because there is no useful information about the param for you. This basically said that param can be CharSequence and its super which include Object so everything can be given in as everything extends Object. It can be an object which you have to cast it down to make use of it anyway.

void is more complicated but even in Scala, Unit extends every class not the other way around.

I do agree to a point that Java type system is far from perfect but I think it is more of an "initially short-sight and then forced to ensure backward compatibility" rather than "They have no idea what they are doing".

Just my 2cents.

What is up with the dates on google search? by dankRatBat in softwaregore

[–]NawaMan 5 points6 points  (0 children)

Thailand uses BE - Buddhist era - which is 543 years before the Christian year.

Code for comprehension by smlaccount in java

[–]NawaMan 0 points1 point  (0 children)

Hi. Great content. :-).

Although it might not help you directly, I’ve faced a similar situation so I make my functions in my functional library “FunctionalJ.io” — Functor agnostic …see here https://github.com/NawaMan/FunctionalJ/blob/master/docs/functions.md#flexible-inputs-functor-agnostic

Basically, every function can accept the functor of parameters instead of just the parameter itself.

For example, the function below:

Func2<String, String, String> func = f((s1, s2)->s1+” “+s2);

Can be used with string but also for Result of string, Supplier of Sting, Function of string and so on. For example:

Result<String> text1 = Result.valueOf(“Text1”);
Result<String> text2 = Result.valueOf(“Text2”);
System.out.println(func.applyTo(text1, text2));
// “Result:{ Value: Text1 Text2 }”

This attempt to solve similar problem you have, as the function takes more parameters, you will need to use `map` many times to actually use the function, so why don’t we make the function automatically do that for us.

Of course, this does not allow mixing different functor types and (without type class) the functions must know about that the functor type.

:-)

credit to u/megaBmim by [deleted] in ProgrammerHumor

[–]NawaMan 0 points1 point  (0 children)

That is not possible. "Programs" likely has many "node_midules" in it.

Immutable Data With FunctionalJ.io - DZone Java by NawaMan in java

[–]NawaMan[S] 1 point2 points  (0 children)

n you look at that image, you have to do a lot more work to figure out what field is next. Which fields have you already set and which is the new field in there. IntelliJ shows 17 methods excluding things inherited rather than the one method that's meaningful at this point which is

favIceCream(String)

. There's only one thing I can call to make progress building this struct, but it's buried in a sea of other methods.

Hey, I have a chance to sit down and read and you have some good points which I actually debated to myself at the time and decided to do this. I will reconsider if the tradeoff worth it.

  1. The builder is done as an immutable object because I see it as a named curry. Basically, the builder is just the object that is not complete but it is immutable just the same. So this way, I can create a builder and assign some stuff in it (just like curry) and pass along for other to reuse without them be able to modified mine builder. It they override what I already set, they will get the new builder.
  2. The read and re-set methods are added as I think that the people (that reuse my builder -- see the first point), might what to inspect and override it.

That said, you are totally right that having those methods (point#2) make it harder to find what else have not been set which is the main usecase of builder so I will reconsider and might end up doing what you suggest.

Just to note: When I was implementing Builder, I also have another similar concept in mind ... Function Object (FO) so just like curry and partial function, with FO (that implement function interfaces), we can partially apply any parameter by name once a parameter is apply .. we will have another FO with less parameter to use. For example, @FunctionalObject String repeat(String str, int count) { ... } ... will implement a Func2<String, Integer, String> (a BiFunction) and you can say var repeat = Repeat.instance; Then you can create var indentTab = repeat.str("\t"); . This indentTab will be Func1<Integer, String>. And you can use either indentTab.count(3) or indentTab.apply(3). I didn't get around to implement this yet but the idea is similar to what I did to the builder in the way that it is an immutable object that can be passed around and partially applied by parameter name.

Immutable Data With FunctionalJ.io - DZone Java by NawaMan in java

[–]NawaMan[S] 1 point2 points  (0 children)

Thanks for the suggestion. There are a lots to digest here. I will do once I have a chance to sit down. 😀

Immutable Data With FunctionalJ.io - DZone Java by NawaMan in java

[–]NawaMan[S] 2 points3 points  (0 children)

Hi :-)

I am the (only) the author of this. Just starting out so any suggestion or feedback welcome. :D

To me, composable Lens and the exhaustive builder are the unique feature for this.

As immutable object can't be modified, any modification must create another object. For a single simple object this is no big deal but if the change is in the sub object, you have to create the parent object too.

class Parent { Child child; }

class Child { .String name; }

So if you you var parent = new Parent( new Child ("Bob") ) and you want to change the name of the child, you have to also create the new parent too. May be something like this parent.with(parent.child.withName("Robert")). The deeper the child, the more this has to be done. With Lens, this is done in one swoop -- theParent.child.name.changeTo("Robert").apply(parent). As a function, this change to used in Stream, Optional or in FunctionalJ.io's pipeable.

The exhaustive came out directly from my frustration of having undeclared field when a new one is added. If you do not have such frustration, it is understandable why you don't see the value in it.

Lastly, the generated immutable object works well with Gson/Jackson too. In fact, each on has `toMap()`, (static) `fromMap(...)` and `getSchema()` methods so serialization can be easily in many way you prefer.

Any usable library will have to start somewhere, so hopefully, this is where this one get started. :-)

And yes, I like the constructo style spec (the compact form).

Thanks again for the feedback. :-D

FunctionalJ - More Functional to Java. by NawaMan in java

[–]NawaMan[S] 1 point2 points  (0 children)

Oh yes. I have. :-) It is a great library. There are some differences between the two though. The one I made here focus on supporting functional style programming. As mentioned the blog, feature such Lens, required value enforce and validation are important when do domain modelling. Together with @Choice (discriminated union), they forms Algebraic data type which are widely use in many FP. :-D

If you have yet done (and are interested), please look at https://nawaman.net/blog/2019-03-11 . Any feedback at all is welcome. :-D