About
import scala.math._ import scala.collection.mutable._ import scala.util.control.Breaks._
object higherorder{ def main(args: Array[String]): Unit = { val list3 = ListBuffer[Int] val list = List(10,7,4,1) println(dropUntil((x => x%2 ==0), list)) val list2 = List(1,2,3,4,1,3,4) println(onlyincreasing(list2)) println(summore(square,1,3,1)) }
def dropUntil(f:Int=>Boolean, list: List[Int]): List[Int] ={
list match{
case Nil => Nil
case head :: tail =>
if (f(head)) {
head :: dropUntil(f, tail)
}
else{
dropUntil(f,tail)
}
}
}
def onlyincreasing(list: List[Int]): List[Int] = {
def lefthelper(list:List[Int], element: Int): List[Int] ={
list match{
case Nil => Nil
case head :: tail =>
if (head > element){
head :: lefthelper(tail, head)
}
else{
Nil
}
}
}
list match {
case Nil => Nil
case head :: tail =>
head :: lefthelper(tail, head)
}
}
def summore(f: Int => Int, a: Int, b: Int, next: Int): Int ={
def helper(f: Int => Int, value: Int, end: Int, next: Int): Int={
if(value<= end){
f(value) + helper(f, value+next, end,next)
}
else{
0
}
}
helper(f,a,b,1)
}
def square(a: Int): Int ={
return a*a
}
}
import java.lang.Thread; import java.util.List; import java.util.ArrayList; /* class Joinacc{ private int balance; public Joinacc(int balance){ this.balance = balance; }
public synchronized void deposit(String custname, int depositamt){
balance += depositamt;
System.out.println(custname +" deposited" + depositamt +" balance is " + balance);
}
public int enquire(String custname){
return balance;
}
}
class Customer extends Thread{ private String custname; private int depositamt; private Joinacc ja;
public Customer(String custname, int depositamt, Joinacc ja){
this.ja = ja;
this.depositamt = depositamt;
this.custname = custname;
}
public void run(){
ja.deposit(custname, depositamt);
ja.enquire(custname);
}
}
public class main{ public static void main(String[] args){
Joinacc ja = new Joinacc(1000);
Customer c1 = new Customer("karish", 100, ja);
Customer c2 = new Customer("karishhh", 300, ja);
Customer c3 = new Customer("kkkkarish", 600, ja);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);
t1.start();
t2.start();
t3.start();
}
}
//COUNTER program
class Count{
private int counter;
public Count(int counter){
this.counter = counter;
}
public synchronized void increment(){
counter += 1;
System.out.println("the counter is incremented, value is "+counter);
}
public synchronized void decrement(){
counter -= 1;
System.out.println("the counter is decremented, value is "+counter);
}
}
class Increment extends Thread{
private Count c;
public Increment(Count c){
this.c = c;
}
public void run(){
c.increment();
}
}
class Decrement extends Thread{
private Count c;
public Decrement(Count c){
this.c = c;
}
public void run(){
c.decrement();
}
}
public class main{ public static void main(String[] args){ Count c = new Count(10); Increment inc = new Increment(c); Decrement dec = new Decrement(c);
Thread t1 = new Thread(inc);
Thread t2 = new Thread(dec);
t1.start();
t2.start();
}
}
*/
// union intersection program
class Operation{ List<Integer> list3 = new ArrayList<>(); List<Integer> list4 = new ArrayList<>();
public void Operation(List<Integer> list3, List<Integer> list4){
this.list3 = list3;
this.list4 = list4;
}
public void union(List<Integer> list1, List<Integer> list2){
list3 = list1;
for(Integer num:list2){
if (!list3.contains(num)){
list3.add(num);
}
}
System.out.println("union " + list3);
}
public void intersection(List<Integer> list1, List<Integer> list2){
List<Integer> list1Copy = new ArrayList<>(list1);
list4.clear();
for (Integer num : list1Copy) {
if (list2.contains(num)) {
list4.add(num);
}
}
System.out.println("intersection " + list4);
}
}
class Union extends Thread{
private List<Integer> list1;
private List<Integer> list2;
private Operation os;
public Union(List<Integer> list1, List<Integer> list2, Operation os){
this.list1 = list1;
this.list2 = list2;
this.os = os;
}
public void run(){
os.union(list1, list2);
}
}
class Intersection extends Thread{
private List<Integer> list1;
private List<Integer> list2;
private Operation os;
public Intersection(List<Integer> list1, List<Integer> list2, Operation os){
this.list1 = list1;
this.list2 = list2;
this.os = os;
}
public void run(){
os.intersection(list1, list2);
}
}
public class main{ public static void main(String[] args){ List<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(5);
list2.add(6);
Operation op = new Operation();
Union un = new Union(list1, list2, op);
Intersection in = new Intersection(list1, list2, op);
Thread t1 = new Thread(un);
Thread t2 = new Thread(in);
t1.start();
t2.start();
} }