converting one function into multiple - Kotlin - function

how would I go about taking this one function and turning it into multiple functions?
I need to create a local double variable called change and set it to a test value (e.g. 44.77)
Create five separate functions dollars(), quarters(), dimes(), nickels() and pennies().
Each function will:
accept a double amount as an argument
calculate and display that denominations number (e.g. 7 Dollars)
then return the recalculated change (e.g. 0.67)
In Kotlin, the function signature for quarters would be: quarters(myChange: Double): Double
I really wish we didn't have to do separate function as one function is so much easier.
here is my code so far:
fun main(args: Array<String>) {
var remChange = .03 ;
monies(remChange);
}
fun monies(remChange: Double){
var dollars = Math.floor(remChange/1.0);
var remChange1 = remChange-(dollars*1.00);
print(Math.round(dollars*1)/1)
if(dollars<2) println("_Dollar"); else if (dollars>=2) println("_Dollars")
var quarter = Math.floor(remChange1/0.25);
var remChange2 = remChange1-(quarter*0.25);
print(Math.round(quarter*4)/4)
if(quarter<2) println("_Quarter"); else if (quarter>=2) println("_Quarters")
var dime = Math.floor(remChange2 / 0.10);
var remChange3 = remChange2 - (dime*0.10);
print(Math.round(dime*10)/10)
if(dime<2) println("_Dime") else if(dime>=2) println("_Dimes")
var nickel = Math.floor(remChange3 / 0.05);
var remChange4 = remChange3 - (nickel*0.05);
print(Math.round(nickel*20)/20)
if(nickel<2) println("_Nickel") else if (nickel>=2) println("_Nickels")
var penny = Math.floor(remChange4*100);
print(Math.round(penny*100)/100)
if(penny<2) println("_Penny") else if (penny>=2) println("_Pennies")
return Unit;
}
here is my attempt at this:
fun main(args: Array<String>) {
val change = 10.88
Dollar(change);
Quarter(change);
Dime(change);
Nickel(change);
Penny(change);
}
fun Dollar(myChange: Double): Double{
val dollar = myChange/(1.00).toFloat();
val change1 = myChange - (dollar * 1.00);
print((dollar * 1)/1);
if(dollar<2) println("_Dollar"); else if (dollar>=2) println("_Dollars");
return change1
}
fun Quarter(myChange: Double): Double{
val quarter = myChange/(0.25).toFloat();
val change2 = myChange - (quarter * 0.25);
println((quarter*4)/4);
if(quarter<2) println("_Quarter"); else if (quarter>=2) println("_Quarters");
return change2
}
fun Dime(myChange: Double): Double{
val dime = myChange/(0.10).toFloat();
val change3 = myChange - (dime * 0.10);
println((dime * 10)/10);
if(dime<2) println("_Dime"); else if(dime>=2) println("_Dimes");
return change3
}
fun Nickel(myChange: Double): Double{
val nickel = myChange/(0.05).toFloat();
val change4 = myChange - (nickel * 0.05);
println((nickel*20)/20);
if(nickel<2) println("_Nickel"); else if (nickel>=2) println("_Nickels");
return change4
}
fun Penny(myChange: Double){
val penny = myChange/100);
print((penny * 100)/ 100);
if(penny<2) println("_Penny"); else if (penny>=2) println("_Pennies");
return Unit
}
OUTPUT:
Unexpected tokens (use ';' to separate expressions on the same line)
Expecting an element
I can't figure out where the rouge ';' is or lack there of
and I'm not sure why it's missing an element. I just want it to produce the same output as my first code. I've done everything from youtube, w3, chegg, online forum research.

Since this is homework I'm not just going to provide a solution, but here's something important to consider - usually, the point of breaking stuff down into multiple functions is to reduce repetition, and make code reusable by having it as its own function. Check this out, from your solution:
fun Dime(myChange: Double): Double{
val dime = myChange/(0.10).toFloat();
val change3 = myChange - (dime * 0.10);
println((dime * 10)/10);
if(dime<2) println("_Dime"); else if(dime>=2) println("_Dimes");
return change3
}
fun Nickel(myChange: Double): Double{
val nickel = myChange/(0.05).toFloat();
val change4 = myChange - (nickel * 0.05);
println((nickel*20)/20);
if(nickel<2) println("_Nickel"); else if (nickel>=2) println("_Nickels");
return change4
}
Obviously there's a lot of repetition there, which is probably part of the reason you're saying it's just making things harder - it's true, it is! It's bloating the code and making it harder to maintain, because any change in the logic has to be repeated for every function, because it's the same logic, just using different values.
So what you could do, is create one calculation function that handles this logic, using a set of values that's passed in. Then, each denomination's function can call that calculation one, passing in the set of values appropriate for that denomination. So they end up acting as helper functions, "configuring" the main one.
So now the question is, what are those parameters? What is it that changes in each of those functions, what are the things that vary, the variables? You should probably think about it yourself before you read the rest, but I see these:
the value you're dividing by to get a coin count (e.g. 0.05 for nickels)
the value you're multiplying by to get a money total, to work out how much change is remaining (e.g. 0.05 for nickels)
the value you're multiplying your coin total by in the println statement (e.g. 20 for nickels)
the value you're dividing by in the same statement (e.g. 20 for nickels)
singular and plural labels for the denomination (e.g. _Nickel and Nickels for nickels)
Some of those look the same (e.g. 0.05 and 0.05) so maybe you can pass in a single variable, which gets used in two places in the logic. You have to work out if this is the case, if they're the same value being referenced twice, or if it's a coincidence.
Some of those also look related to other values, e.g. 10 * 0.1 = 1.0, 20 * 0.05 = 1.0. If this is the case, and there's a consistent relationship between two values, then maybe you can derive one from the other, meaning you only need to pass one value in, and calculate the others you need to use from that. Again, you need to work out if this is logically sound - you're writing an algorithm here, you need to understand what those values are and what they mean. Breaking stuff down into reusable functions is all about this kind of generalisation and working out how things relate to each other.
Once you have your nice utility function that takes a set of parameters, you can make helper functions that call it with a fixed set of values:
fun nickels(myChange: Double): Double {
return calculate(parameters relating to nickels go here)
}
and now, there's no repetition! The calculation code is in one function, the printing code is in one function, and each of the denomination functions just holds unique data (the parameters passed to the main function).
I wouldn't necessarily tackle this problem like this (and I'm not reviewing the actual logic in your functions either) but it's good as a general example of how you approach things in a more functional way. Basically - if you find yourself repeating things, maybe you can stick that in its own function, and have the repeats call that instead

import kotlin.math.floor
fun dollars(remChange: Double): Double {
return arrayOf(1.0).fold(0.0 to remChange) { acc, dbl ->
val amount = floor(acc.second / dbl) * dbl
amount to acc.second - amount
}.first
}
fun quarters(remChange: Double): Double {
return arrayOf(1.0, 0.25).fold(0.0 to remChange) { acc, dbl ->
val amount = floor(acc.second / dbl) * dbl
amount to acc.second - amount
}.first
}
fun dimes(remChange: Double): Double {
return arrayOf(1.0, 0.25, 0.1).fold(0.0 to remChange) { acc, dbl ->
val amount = floor(acc.second / dbl) * dbl
amount to acc.second - amount
}.first
}
fun nickels(remChange: Double): Double {
return arrayOf(1.0, 0.25, 0.1, 0.05).fold(0.0 to remChange) { acc, dbl ->
val amount = floor(acc.second / dbl) * dbl
amount to acc.second - amount
}.first
}
fun pennies(remChange: Double): Double {
return arrayOf(1.0, 0.25, 0.1, 0.05, 0.01).fold(0.0 to remChange) { acc, dbl ->
val amount = floor(acc.second / dbl) * dbl
amount to acc.second - amount
}.first
}
val amount = 44.77
println("Dollars : " + dollars(amount)) // 44.0
println("Quarters : " + quarters(amount)) // 0.75
println("Dimes : " + dimes(amount)) // 0.0
println("Nickels : " + nickels(amount)) // 0.0
println("Pennies : " + pennies(amount)) // 0.2

Your error is this extra closing parenthesis on this line:
val penny = myChange/100);
I don't know of any online playground that shows you the error in the proper location. If you use an IDE like IntelliJ IDEA, it will highlight the error in your code as you type it with a red squiggly underline, just like a typo in a word processor. I highly recommend using IntelliJ IDEA if you are trying to learn Kotlin. It would have saved you a lot of time here.
Regarding your homework task, I think the point of challenging you to break your function up is to remove the repetition, which is a core principle of effective programming. Your new solution breaks it up into multiple functions without removing the repetition, so that's why it seems like a less sensible solution to you. See #cactustictacs' answer for more info about that.

Related

Looping through a circular buffer in Chisel

Let's say I have implemented a circular buffer with a head and a tail. Is there an elegant scala-way of looping through this buffer starting from the head ending at the tail (and doing a possible wrap-around at the end)
class MyBuffer() extends Module
{
val data = Reg(Vec(NUM_ELEMENTS, Bool())
val head = RegInit(0.U(NUM_ELEMENTS_W.W))
val tail = RegInit(0.U(NUM_ELEMENTS_W.W))
}
I'm not sure what your looping goal is. But consider the following code example (with a few details left out). This example exposes the contents of a RingBuffer as Vec view with ViewLength valid elements. I think this demonstrates a modestly elegant method for this definition of looping, the emitted hardware (or the view idea) may not be elegant. Let me know if is not quite the notion of looping you had in mind.
import chisel3._
import chisel3.util.log2Ceil
/**
* This ring buffer presents its current contents through view
*
* #param depth
* #param bitWidth
*/
class RingBuffer(depth: Int, bitWidth: Int) extends MultiIOModule {
/*
You need a bunch of IO's here to push and pop and get full status
*/
val view = IO(Output(Vec(depth, UInt(bitWidth.W))))
val viewLength = IO(Output(UInt(log2Ceil(depth).W)))
val data = Reg(Vec(depth, Bool()))
val head = RegInit(0.U(bitWidth.W))
val tail = RegInit(0.U(bitWidth.W))
/* Need some code here to push and pop elements */
// this constructs a mapping between the indices between current head and tail
// to the 0 to n indices of the view
def mappedIndex(i: Int): UInt = {
val out = Wire(UInt(log2Ceil(depth).W))
when((i.U + head) <= depth.U) {
out := i.U + head
}.otherwise {
out := (i.U + head) - depth.U
}
out
}
// This creates the complicated Mux structure to map betweem
// the ring buffer elements and 0 to n style view
view.zipWithIndex.foreach { case (viewElement, index) =>
viewElement := data(mappedIndex(index))
}
// This presents the number of valid elements in the current view
val difference = tail - head
when((difference) < 0.U) {
viewLength := (difference) + depth.U
}.otherwise {
viewLength := (difference)
}
}

Optimizing/reducing pure functions with same in/out types to combine them in a simpler pure function?

Disclaimer: I have almost no mathematics notions, so this question could be very basic for some of you.
I'm searching for the name of a concept which consists in combining pure functions together (say, functions with the same input and output types and number of parameters) to make them simpler.
Suppose I have these 3 methods with the same signature:
addOne(param: number): number {
return param + 1;
}
addTwo(param: number): number {
return param + 2;
}
multiplyByThree(param: number): number {
return param * 3;
}
Now I know that I'll always use these functions in the same order and same param.
Ex: I will process a sound or an image.
I want to avoid uselessly applying coefficient or offsets that could be computed together (optimization/regression).
Let's say I have this imaginary library with a method called computeOptimizedFunction that applies this concept to my functions. It takes any number of functions with the same signature as input.
var optimized = computeOptimizedFunction(addOne, addTwo, multiplyByThree);
Actually equals to:
var optimized = (param: number) => 3 * (param + 3);
Anyone here has an idea of how this concept or pattern is called, if it exists?

How to use factory const constructors?

To test my understanding of Dart, I made a 2D immutable vector that stores not only its x and y components, but also its angle and length. They are computed only at construction out of the x and y values. I soon realized that final fields need to be set with initializer lists or the this-parameter shortcut, which don't allow for much computed values. Like this answer points out, I had to use a factory constructor to create my vector out of its x and y components. The factory constructor then validates input and computes the length and angle before calling the private constructor.
import 'dart:math';
class ImmutableVector {
// private fields
final double _x;
final double _y;
final double _angle;
final double _length;
// getters
double get x => _x;
double get y => _y;
double get angle => _angle;
double get length => _length;
/// Constructs a vector out of cartesian components.
///
/// Converts both arguments to doubles.
/// Null values will be treated as zeroes.
factory ImmutableVector.xy(num x, num y) {
x ??= 0.0;
y ??= 0.0;
x = x.toDouble();
y = y.toDouble();
var angle = atan2(y, x) % (2*PI);
var length = sqrt(pow(x, 2) + pow(y, 2));
return new ImmutableVector._raw(x, y, angle, length);
}
/// Constructs a vector by setting the fields directly.
const ImmutableVector._raw(this._x, this._y, this._angle, this._length);
}
But it came to my attention that I could not make the factory constructor const, because const factories can only be redirecting constructors. Is there absolutely no way to make my vector have code in its constructor and still have it an immutable with const constructors? And if so, why?
I'll also mention that I used to throw errors if the values passed were null, but I made it default to zero so I could actually use initializer lists. I then tried to do this, and turns out it works when the constructor isn't a factory:
ImmutableVector.xy(num x, num y) : this._raw(
x = (x ?? 0.0).toDouble(),
y = (y ?? 0.0).toDouble(),
atan2(y, x) % (2*PI),
sqrt(pow(x, 2) + pow(y, 2)));
But as soon as I try to make it const, it tells me that the code in the initializer list contains non-compile-time constants.
The only immutable vector I could find in dart was here on GitHub, and it doesn't do any kind of null-validation or computation on constructor parameters, relying entirely on the fact that methods will break at some point on null-vectors. It also has a constructor that creates a unit vector out of another one with poor performance and repetition thanks to the obligatory initializer list:
const Vector.unit(final num x, final num y, final num z) :
this(x / PMath.len(x, y, z), y / PMath.len(x, y, z), z / PMath.len(x, y, z));
So what should be my conclusion? Did I miss a feature that makes this possible, or should I just give up on using const for this class?
Dart doesn't execute Dart code during compilation. This is the reason const constructors can't have a body and why there is no other way to work around this limitation.
If you want to execute code when an instance is created, just don't make use of const. Const isn't that important in Dart anyway. There were even discussions to remove it from the language because the benefits are not big enough.

Can someone explain this code for me? Base-Conversion Code

So for a homework assignment we had to make a program that converted a number from one base to another (i.e. 110 in base 2 to 6 in base 10). I asked my friend how he did his because I was having trouble and he just sent me his code and nothing else. Can someone explain the logic of this code so that I can make my own program and actually understand how to do this problem. Thanks!
import java.util.*;
public class Base_Converter {
public static final String value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String args[]){
int x, y;
String num, base10 = "";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number you want to convert.");
num = scan.nextLine();
num = num.toUpperCase();
System.out.println("What base is it in?");
x = scan.nextInt();
System.out.println("What base do you want to convert it to?");
y = scan.nextInt();
if(x <= 36 && y <= 36 && x > 1 && y > 1){
base10 = toBase10(num,x);
num = newBase(base10,y);
System.out.println(num);
}
}
public static String toBase10(String num, int from){
long total = 0;
int counter = num.length();
char[] stringArray = num.toCharArray();
for(char w : stringArray){
counter--;
total += value.indexOf(w)*Math.pow(from,counter);
}
return String.valueOf(total);
}
public static String newBase(String num, int to){
String total = "";
int current = 0;
while(Integer.valueOf(num) > 0){
current = Integer.valueOf(num)%to;
total = value.charAt(current)+total;
num = String.valueOf(Integer.valueOf(num)/to);
}
return total;
}
}
I think you should be focusing not on what your friend's code does, but instead with how to do the assignment yourself, because I think your problems lie with a lack of understanding on your part. Instead of leaving you high and dry though I'll walk you through some of the specifics of base-conversion.
First, read user input. It looks like you're using Java, so just use a scanner to do this. At minimum you'll want to read the number you're converting, what base it is in, and what base the output will be in.
Next, we want to convert the number. You could directly convert numbers to each other (i.e. converting base 2 to base 8) but that requires more brainpower than I am willing to offer right now. Instead, I would suggest always first converting the user-inputted number to base 10 (much like your friend did). So how do we convert a number of an unknown base to base 10?
So let's break down how a number is represented: lets say we have the number 234 in base ten. This is equivalent to 4*10^0 + 3*10^1 + 2*10^2 or 4 + 30 + 200 = 234. You can use this same conversion for any other numbers. I.E. if the number is 1763 in base 8, the value in base 10 will be 3*8^0 + 6*8^1 + 7*8^2 + 1*8^3 or 3 + 48 + 448 + 512 = 1011 base 10(try entering 1763 here for proof. So to convert to decimal, you just need to see to multiply each individual number time your base to the power of its place minus 1. For example, since 1 is the fourth number in the 1763 you multiply it times 8^(4-1). Since, you are reading a string from the user. You'll need to convert each character of the string to an integer using the ascii chart.
Now to convert from base ten to anything. Instead of multiplying, you just divide each value and write the remainder! I'll let someone else describe this procedure.
Now just store this new value as a string doing somethings like
String output = "";
output += newValue;
In computer science, just copying someone else's code is way more harmful than helpful. Hope this helps!

SML: declear but not define function

This is just an example. These two functions are connected. You really want to call the one called lowest. It should then return the lowest number of the two. This of course won't work because you at the time of compiling the reducemax function you make a call to an at the time undefined function called lowest.
fun reducemax (i:int * int):int =
if (#1 i) > (#2 i)
then lowest(((#1 i)-1), (#2 i))
else lowest((#1 i), ((#2 i)-1));
fun lowest (i:int * int):int =
if (#1 i) = (#2 i)
then (#1 i)
else reducemax((#1 i), (#2 i));
I know I can use let to declare the reducemax function inside lowest, but is there any way around this? For example like in C, declare the function without defining it. I do understand the reducemax function need to know lowest will return an int and taking a int * int argument.
Just replace the second fun with and and remove the semicolons. This defines mutually recursive functions.