LinearArray, now i get it, do i?
reverendsteveii, throw new SameJokeException();
JayDee, Is it possible to get the joke at runtime using the spectre exploit?
coloredgrayscale, Not required. Looks like Java, just use reflection.
Xylight, i hate this programming pattern with a passion.
someonesmall, Setters and Getters?
purplemonkeymad, Where getter?
mexicancartel, Well you wouldn’t get it
Xylight, yes.
someonesmall, So what is a better paradigm in your opinion?
sudo, Immutable members. Set in constructor then read only. The Builder pattern is acceptable if you’re language is an obstacle.
AVincentInSpace, found the functional programming purist
sudo, ✅
Piafraus, So do you create new objects every time you need to change state?
sudo, You avoid having mutable state as much as possible. This is a pretty standard concept these days.
Piafraus, Can you please give me an example - let’s say I have a big list of numbers and I need to find how many times each number is there.
I would expect a mutable dictionary/map and a single pass through. How would you do that without mutable datastructure?
sudo, (edited ) Very standard use case for a
fold
orreduce
function with an immutable Map as the accumulator<span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">ints </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">List</span><span style="color:#323232;">(</span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="color:#0086b3;">2</span><span style="color:#323232;">, </span><span style="color:#0086b3;">2</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">sum </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> ints.foldLeft(</span><span style="color:#0086b3;">0</span><span style="color:#323232;">)(_ + _) </span><span style="font-style:italic;color:#969896;">// 14 </span><span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">counts </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> ints.foldLeft(</span><span style="color:#0086b3;">Map</span><span style="color:#323232;">.empty[</span><span style="font-weight:bold;color:#a71d5d;">Int</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">Int</span><span style="color:#323232;">])((c, x) </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">{ </span><span style="color:#323232;"> c.updated(x , c.getOrElse(x, </span><span style="color:#0086b3;">0</span><span style="color:#323232;">) + </span><span style="color:#0086b3;">1</span><span style="color:#323232;">) </span><span style="color:#323232;">}) </span>
foldLeft
is a classic higher order function. Every functional programming language will have this plus multiple variants of it in their standard library. Newer non-functional programing languages will have it too. Writing implementations offoldLeft
andfoldRight
is standard for learning recursive functions.The lambda is applied to the initial value (0 or
Map.empty[Int, Int]
) and the first item in the list. The return type of the lambda must be the same type as the initial value. It then repeats the processes on the second value in the list, but using the previous result, and so on until theres no more items.In the example above,
c
will change like you’d expect a mutable solution would but its a new Map each time. This might sound inefficient but its not really. Because each Map is immutable it can be optimized to share memory of the past Maps it was constructed from. Thats something you absolutely cannot do if your structures are mutable.
Piafraus, So you have memory space which is reused… Which essentially makes it a mutable memory structure, where you update or add with new data keys… No?
sudo, No. Persistent Data Structures are not mutable. The memory space of an older version is not rewritten, it is referenced by the newer version as a part of its definition. ie via composition. It can only safely do this if the data it references is guaranteed to not change.
<span style="color:#323232;">x </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">2 </span><span style="font-weight:bold;color:#a71d5d;">:: </span><span style="color:#0086b3;">1 </span><span style="font-weight:bold;color:#a71d5d;">:: </span><span style="color:#0086b3;">Nil </span><span style="font-style:italic;color:#969896;">-- [2, 1] </span><span style="color:#323232;">y </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">3 </span><span style="font-weight:bold;color:#a71d5d;">::</span><span style="color:#323232;"> x </span><span style="font-style:italic;color:#969896;">-- [3, 2, 1] </span>
In this example both
x
andy
are single linked lists.y
is a node with value3
and a pointer tox
. Ifx
was mutable then changingx
would changey
. That’s bad™ so its not allowed.If you want to learn more about functional programming I suggest reading Structures and Interpretation of Computer Programs or Learn You a Haskell for Great Good
Xylight, immutable objects, i like functional programming
intensely_human, Java?
BassaForte,
public Joke Joke { private get; set; }
sik0fewl, Upon reflection, I do get the joke now.
rimjob_rainer, This one gets it
Kolanaki, Stop making private jokes and start posting them publicly. We wanna laugh too, ya selfish bastid.
JustBrian7872, They don’t call me AbstractJokerAdapterFactoryProxy for nothin’
hydroptic, Where are your gods now?
<span style="font-weight:bold;color:#a71d5d;">public static </span><span style="color:#0086b3;">Joke </span><span style="color:#323232;">getTheJoke(</span><span style="color:#0086b3;">Meme</span><span style="color:#323232;"> yourMeme) { </span><span style="color:#323232;"> </span><span style="color:#0086b3;">Field</span><span style="color:#323232;"> jokeField </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">Meme</span><span style="color:#323232;">.class.getDeclaredField(</span><span style="color:#183691;">"joke"</span><span style="color:#323232;">); </span><span style="color:#323232;"> jokeField.setAccessible(</span><span style="color:#0086b3;">true</span><span style="color:#323232;">); </span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">return </span><span style="color:#323232;">(</span><span style="color:#0086b3;">Joke</span><span style="color:#323232;">) jokeField.get(yourMeme); </span><span style="color:#323232;">} </span>
RonSijm, (edited ) Is it Java? It looked like Microsoft Java C# to me…
<span style="color:#323232;"> public static void Main(string[] args) </span><span style="color:#323232;"> { </span><span style="color:#323232;"> var meme = new Meme(); </span><span style="color:#323232;"> var joke = GetTheJoke(meme); </span><span style="color:#323232;"> } </span><span style="color:#323232;"> </span><span style="color:#323232;"> public static Joke GetTheJoke(Meme theMeme) </span><span style="color:#323232;"> { </span><span style="color:#323232;"> var memeType = typeof(Meme); </span><span style="color:#323232;"> var jokeField = memeType.GetField("Joke", BindingFlags.NonPublic | BindingFlags.Instance); </span><span style="color:#323232;"> return (Joke)jokeField.GetValue(theMeme); </span><span style="color:#323232;"> } </span>
rimjob_rainer, Because C# is a Java clone
noproblemmy, If you have to cast your joke it isn’t funny?
Karyoplasma, Could just change it to public static Object GetTheJoke, no?
hydroptic, Frankly it’s been a while since I wrote either one. I just assumed Java because of the naming convention, and I didn’t see anything I took as obviously un-Java in the class definition
PoolloverNathan, There isn’t an unnecessary level of capitalization; seems to be regular Java with Allman braces.
Hazzia, Is it Java?
Wait a minute, that’s an actual thing in java!? What the fuck Java I already didn’t like you and now you start pulling this shit? What even is the point of creating standards if you design backdoors to them
rimjob_rainer, (edited ) Reflection is sometimes a necessary evil. At least it makes it harder to abuse the class and if you do, then you are responsible if something goes wrong.
RonSijm, (edited ) Yea, what @hydroptic posted is actually Java
What even is the point of creating standards if you design backdoors to them
If you’re building in a backdoor anyways, why would the backdoor require 5 lines of weird reflection to get the type, type info, fieldinfo with the correct binding flags, and then invoking the method?
I think it’s kinda neat compared to C#, just being able to say “Ignore private/protected/internal keywords”
hydroptic, (edited ) If you want to be able to eg. (de)serialize non-public fields of a type for any reason, you’ll need some way to get around the access restriction. Mocking is another use case – although it’s a philosophical discussion whether you should be mocking non-public fields.
And this isn’t just a Java thing, the comment you’re responding to has an example in C#, and you can do something similar in a lot of languages that support runtime reflection. Barring runtime reflection support you can do pointer math if the language supports it. Access restrictions on fields are there to stop casual misuse of private fields, but sometimes you actually may want to be able to step over those restrictions if you really know what you’re doing.
victorz, Ha. Cause there’s no getter. I get it. I think?
4am, It’s also an inside Joke
intensely_human, And the Joker gets it, but you don’t.
dohpaz42, I get it.
No you don’t; there’s no getter.
victorz, Oh, now I get it.
Wait…
Batman, You don’t get the context of this joke
fsxylo, var context = getContext();
lightnegative, var context = RuntimeSingletonFactory.getCurrentFactory().getCurrentRuntimeSingleton().getContext()
Add comment