From 9e54ee7a80f53de3bbcb505b26f53adb7a5fb704 Mon Sep 17 00:00:00 2001
From: "Andrew E. Torda" <torda@zbh.uni-hamburg.de>
Date: Thu, 27 Jan 2022 11:37:00 +0100
Subject: [PATCH] Got rid of no-op Close function and the complicated types.
 Now use a type assertion.

---
 mc_work/dorun.go | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/mc_work/dorun.go b/mc_work/dorun.go
index 01ad5a1..c61d01c 100644
--- a/mc_work/dorun.go
+++ b/mc_work/dorun.go
@@ -12,7 +12,7 @@ import (
 	"sync"
 
 	"example.com/ackley_mc/ackley"
-	"example.com/ackley_mc/ui"
+	//	"example.com/ackley_mc/ui"
 )
 
 const (
@@ -33,9 +33,9 @@ func getSeed() int64 {
 	return r
 }
 
-type withCloser struct{ io.Writer }   // We have to promote him to have a Close()
-func (withCloser) Close() error       { return nil }
-func addClose(b io.Writer) withCloser { return withCloser{b} }
+type withBytes interface {
+	Bytes() []byte
+}
 
 type cprm struct { // parameters calculated from input
 	rand       *rand.Rand
@@ -48,8 +48,7 @@ type cprm struct { // parameters calculated from input
 	plotTmprtr []float64      // Temperature values for plotting
 	plotXtrj   []float32      // for plotting trajectories
 	coolme     bool           // Cooling ? or just constant temperature ?
-	fplotWrt   io.WriteCloser // Where to write the plot of function values
-	fplotbuf   *bytes.Buffer  // The buffer possibly behind fplotWrt
+	fplotWrt   io.Writer      // Where to write the plot of function values
 	xplotme    bool           // Are we plotting X trajectories
 }
 
@@ -109,9 +108,8 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
 			return err
 		}
 	} else { // Must be writing to a screen
-		var w bytes.Buffer           // could be fancy, preallocate and use bytes.Newbuffer
-		cprm.fplotWrt = addClose(&w) // Fix this, effectively two copies
-		cprm.fplotbuf = &w         // of the pointer
+		var w bytes.Buffer // could be fancy, preallocate and use bytes.Newbuffer
+		cprm.fplotWrt = &w
 	}
 	n_alloc := mcPrm.nStep / 5 // About a fifth of the number of steps
 	cprm.plotnstp = make([]float64, 0, n_alloc)
@@ -199,7 +197,7 @@ func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32
 	}
 	return xDlta
 }
-func breaker() {}
+func breaker(...interface{}) {}
 
 // doRun does a Monte Carlo run. Although single precision is fine for the
 // coordinates and function, we use double precision for the temperature.
@@ -215,8 +213,8 @@ func doRun(mcPrm *mcPrm) error {
 		defer cprm.fOut.Flush()
 	}
 
-	if cprm.fplotWrt != nil {
-		defer cprm.fplotWrt.Close()
+	if c, ok := cprm.fplotWrt.(io.Closer); ok == true { // will also have to do for X values
+		defer c.Close()
 	}
 
 	var nAcc int                           // Counter, number of accepted moves
@@ -273,9 +271,8 @@ func doRun(mcPrm *mcPrm) error {
 		return err
 	}
 
-	if cprm.fplotbuf != nil {
-		fmt.Println("len", cprm.fplotbuf.Len())
-		ui.Scrnplt(cprm.fplotbuf.Bytes())
+	if bBuf, ok := cprm.fplotWrt.(withBytes); ok == true {
+		fmt.Println("bBuf for plotting is this big", len(bBuf.Bytes()))
 	}
 	if err := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil {
 		return err
-- 
GitLab