This commit is contained in:
2025-03-21 17:09:04 +01:00
parent 923a17f7ff
commit ed3e43889e
11 changed files with 278 additions and 0 deletions

View File

@ -0,0 +1,72 @@
// Java program to show working of user defined
// Generic classes
// We use < > to specify Parameter type
class Cat {
String breed;
String name;
boolean castrated;
Cat(String breed, String name, boolean castrated) {
this.breed = breed;
this.name = name;
this.castrated = castrated;
}
public String makeNoise() {
return "Meow";
}
}
class Test<T> {
// An object of type T is declared
T obj;
Test(T obj) {
this.obj = obj;
}
public T getObject() {
return this.obj;
}
}
class AdHocPolymorphic {
public String add(int x, int y) {
return "Sum: " + (x + y);
}
public String add(String name) {
return "Added " + name;
}
}
public class Adhoc {
public static void main(String[] args) {
AdHocPolymorphic poly = new AdHocPolymorphic();
System.out.println(poly.add(1,2)); // prints "Sum: 3"
System.out.println(poly.add("Jay")); // prints "Added Jay"
// error the fuck out
}
}
// Driver class to test above
class Main {
public static void main(String[] args) {
// instance of Integer type
Test<Integer> iObj = new Test<Integer>(15);
System.out.println(iObj.getObject());
// instance of String type
Test<String> sObj = new Test<String>("GeeksForGeeks");
System.out.println(sObj.getObject());
Test<Cat> cObj = new Test<Cat>(new Cat("Siamese", "Fluffy", true));
System.out.println(cObj.getObject().makeNoise());
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,62 @@
z :: Int -> Int
z a = 3 * a -- 3x3 = 9 => 3*3
y :: (Int -> Int) -> [Int] -> [Int]
-- y(\x -> 3*x) [1,2,3] -> [3,6,9]
y z lst = map z lst -- f(x) = 3x => f x = 3x
isEven :: Int -> Bool
isEven x = if x `mod` 2 == 0 then True else False
-- ~~mod(x, 2)~~ => x mod 2
-- isEven x = x `mod` 2 == 0
isAmongus :: String -> String
-- traverse each character and check if "amongus" is in the string, return the rest of the string
isAmongus [] = [] -- base case if the string is empty (or we reached the end of the string)
isAmongus (x : xs) -- recurse on the rest of the string
| (take 7 (x : xs) == "amongus") = drop 7 (x : xs)
| otherwise = isAmongus xs
b :: [Int] -> Int
b lst = sum (y z lst) -- = 18
allTogether :: [Int] -> Int
allTogether list = sum (map (3 *) list)
foo :: Integer -> Integer
foo 0 = 16
foo 1
| "Haskell" > "C++" = 3
| otherwise = 4
foo n
| n < 0 = 0
| n `mod` 17 == 2 = -43
| otherwise = n + 3
bogus :: [a] -> Bool
bogus (x:_) = True
bogus _ = False
main :: IO ()
main = do
print $ b [1, 2, 3]
putStrLn "AAAAAAAAA"
print $ allTogether [1, 2, 3]
putStrLn "Finding amongus"
print $ isAmongus "amongus"
print $ isAmongus "aaaaaaaaaaaaaa"
print $ isAmongus "askldhjewkjrhwekjrhwekjrhekjlfhbjerkshtjkernamogusejqwklwqhekqjwehqwjkamongusasdasdsad"
-- foos
print $ foo 0
print $ bogus [1,10..]
-- compose 2 functions with a dot
print $ (y z . y z) [1, 2, 3]
print $ (y z . y z . y z) [1, 2, 3]
print $ (.) (y z) (y z) [1, 2, 3]
print $ y z $ y z [1, 2, 3]

Binary file not shown.